$str= 'We have new Call Request: Reference = 55823014, Name = Amal, Mobile = 111111'
如何从字符串中提取此部分,如:
reference | 55823014
Name | Amal
Mobile | 1111111
答案 0 :(得分:2)
使用正则表达式提取
$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;
这会给你
Array
(
[0] => Reference = 55823014, Name = Amal, Mobile = 111111
[1] => 55823014
[2] => Amal
[3] => 111111
)
Reference = 55823014
Name = Amal
Mobile = 111111
<强> Demo 强>
答案 1 :(得分:1)
首先删除文字&#39;我们有新的通话请求:&#39;从字符串。然后,您将获得包含键值对的主字符串。从那里,您可以用逗号将其分解为一个标记数组,其中每个标记包含一个键值对。然后你遍历标记并通过用等于&#39; =&#39;爆炸它来分离出键值。标志。这是代码:
<?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);
答案 2 :(得分:0)
好的作为快速方法,这应该有效:
$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;
}
输出:
Reference | 55823014
Name | Amal
Mobile | 111111