Martin Männimets
Martin Männimets

Reputation: 11

Php number out of text?

Im new here. I need help with php.

$FullDescriptionLine = Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|

how can i get 23" out of that string?

Thank you Martin

Upvotes: 1

Views: 48

Answers (5)

Madhur Bhaiya
Madhur Bhaiya

Reputation: 28844

You can use explode function. It converts a given string into array elements, separated by a delimiter. It takes two inputs, a delimiter string ('|') and the string to convert into array chunks ($FullDescriptionLine).

Now, in your case, 23" is in the second substring (array index 1 - remember that array indexing start from 0). Post exploding the string, you can get the value using index [1].

Try the following (Rextester DEMO):

$FullDescriptionLine = 'Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|';

// explode the string and access the value
$result = explode('|', $FullDescriptionLine)[1];
echo $result;  // displays 23"

Upvotes: 1

user3783243
user3783243

Reputation: 5224

For structured strings I recommend str_getcsv and then use the second parameter to define the delimiter.

$array = str_getcsv('Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|', '|');
echo $array[1];

https://3v4l.org/7XSDQ

Upvotes: 1

Mohammad
Mohammad

Reputation: 21489

Using explode() is simple and good that multiple user posted it. But there is another way. Using regex in preg_match()

preg_match("/\|([^|]+)/", $FullDescriptionLine, $matches);
echo $matches[1];

Check result in demo

Upvotes: 0

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38532

Just explode() it with pipe | character and grab the first i.e 1st index as array starts from 0 index.

<?php
  $FullDescriptionLine = 'Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|';
  $array = explode('|',$FullDescriptionLine);
  //just for debug and clearly understand it
  print '<pre>';
  print_r($array);
  print '</pre>';
  echo $array[1];
 ?>

DEMO: https://3v4l.org/8aGuO

Upvotes: 3

Ethan
Ethan

Reputation: 4375

Use PHP's explode function to split the string on pipes (|), then get the second index (1 in computer indexes) of that array.

$FullDescriptionLine = 'Model 23MP48HQ-P|23"|Panel IPS|Resolution 1920x1080|Form factor 16:9|';
echo explode('|', $FullDescriptionLine)[1];

Online PHP demo

Upvotes: 3

Related Questions