Reputation: 11
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
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
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];
Upvotes: 1
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
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