ホームページ >バックエンド開発 >PHPチュートリアル >文字列を抽出する
请问 如何把字符串
48d379e8ae9371b56065a88964533189 5c0ff9ef0f5ac999140564542e4a3b271000000077946022d380b9349108b780eae55ff7490127bfc65cbc24fc906d149b11815370a02a5c25eaa9e19a7456744e217707527ff00 5c0ff9ef0f5ac999140564542e4a3b271000000077946122d380b9349108b780eae55ff7490127bfc65cbc24fc906d149b11815370a02a11c25eaa9e19a7456744e217707527ff00
中的orderNo和orderResult标签的数据全部提取出来。分别放到两个数组中?
$str = "<?xml version='1.0' encoding='gb2312'?><orderNo>10000000779460</orderNo><orderResult>5</orderResult><orderNo>10000000779461</orderNo><orderResult>11</orderResult>";htm;preg_match_all("/<([^>]+)>([^<]+)<\/\\1>/U", $str, $matches);$out = array();foreach ($matches[1] as $key => $value) { $out[$value][] = $matches[2][$key];}var_dump($out);
不是标准的xml输出吧?
$s = "48d379e8ae9371b56065a88964533189 5e4f0452c956abb82e6662046a0f66305c0ff9ef0f5ac999140564542e4a3b271000000077946022d380b9349108b780eae55ff7490127bfc65cbc24fc906d149b11815370a02a5c25eaa9e19a7456744e217707527ff00 5c0ff9ef0f5ac999140564542e4a3b271000000077946122d380b9349108b780eae55ff7490127bfc65cbc24fc906d149b11815370a02a11c25eaa9e19a7456744e217707527ff00 e4a958d5d76049640eeb8862309d22ea";
$a = simplexml_load_string($s);
print_r($a);
比如 我要把所有5c0ff9ef0f5ac999140564542e4a3b271000000077946022d380b9349108b780eae55ff7490127这样的标签的数据放到$array_order[] 数组
把bfc65cbc24fc906d149b11815370a02a11c25eaa9e19a7456744e217707527ff00标签的数据放到$arraresult[] 数组中
我是从配送公司获取的这个字符串。目的就是要把5c0ff9ef0f5ac999140564542e4a3b27 和bfc65cbc24fc906d149b11815370a02a 这两个标签的数据存入两个数组中
//xml 文件操作$simXml = simplexml_load_file('xmlpath');//取出orderNo$orderNo = array();foreach ($simXml->orderNo as $order) { $orderNo[] = (string)$order; //直接转换成字符串存放}//取出orderResult$orderResult = array();foreach ($simXml->orderResult as $res) { $orderResult[] = (string)$res;//直接转换成字符串存放,也可以转换成其他的;}
$s =<<< TXT<?xml version='1.0' encoding='gb2312'?> <orderNo>10000000779460</orderNo><orderResult>5</orderResult> <orderNo>10000000779461</orderNo><orderResult>11</orderResult>TXT;$d = array('orderNo' => 'array_order', 'orderResult' => 'arraresult');preg_match_all('#<(orderNo|orderResult)>(.+)</\1>#U', $s, $r);foreach($r[1] as $i=>$k) ${$d[$k]}[] = $r[2][$i];print_r($array_order);print_r($arraresult);
Array( [0] => 10000000779460 [1] => 10000000779461)Array( [0] => 5 [1] => 11)