$str= 'We have new Call Request: Reference = 55823014, Name = Amal, Mobile = 111111'
How do i extract this part from the string like:
reference | 55823014
Name | Amal
Mobile | 1111111
$str= 'We have new Call Request: Reference = 55823014, Name = Amal, Mobile = 111111'
How do i extract this part from the string like:
reference | 55823014
Name | Amal
Mobile | 1111111
use Regular Expressions to extract that
$str= 'We have new Call Request: Reference = 55823014, Name = Amal, Mobile = 111111';
preg_match('/Reference =(.*?), Name =(.*?), Mobile =(.*)/', $str, $m);
print_r($m);
//if you want to display an item at a time
echo "Reference = ".$m[1].PHP_EOL;
echo "Name = ".$m[2].PHP_EOL;
echo "Mobile = ".$m[3].PHP_EOL;
this will give you
Array
(
[0] => Reference = 55823014, Name = Amal, Mobile = 111111
[1] => 55823014
[2] => Amal
[3] => 111111
)
Reference = 55823014
Name = Amal
Mobile = 111111
First you remove the text 'We have new Call Request:' from the string. Then you are left with the main string that contains the key value pairs. From that, you explode it into an array of tokens by comma where each token contains one key value pair. Then you loop through the tokens and separate out the key value by exploding it with equal to '=' sign. Here's the code for it:
<?php
$str = 'We have new Call Request: Reference = 55823014, Name = Amal, Mobile = 111111';
$str = substr($str, strpos($str, ':') + 1);
$arr = explode(',', $str);
$data = array();
foreach ($arr as $item) {
$tokens = explode('=', $item);
$key = trim($tokens[0]);
$val = trim($tokens[1]);
$data[$key] = $val;
}
var_dump($data);
Okay as a quick approach, this should work:
$str= 'We have new Call Request: Reference = 55823014,
Name = Amal, Mobile = 111111';
$str = strchr($str, "Reference");
$str = explode(',', $str);
foreach ($str as $item) {
$tokens = explode('=', $item);
$key = trim($tokens[0]);
$val = trim($tokens[1]);
echo $key . " | " . $val . PHP_EOL;
}
Output:
Reference | 55823014
Name | Amal
Mobile | 111111