Home  >  Article  >  Backend Development  >  摘取字符串

摘取字符串

WBOY
WBOYOriginal
2016-06-23 14:00:35992browse

请问 如何把字符串 
 100000007794605 1000000077946111
中的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 = " 100000007794605 1000000077946111 ";
$a = simplexml_load_string($s);
print_r($a);

比如 我要把所有10000000779460这样的标签的数据放到$array_order[] 数组
11标签的数据放到$arraresult[]  数组中

我是从配送公司获取的这个字符串。目的就是要把 和 这两个标签的数据存入两个数组中

//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;//直接转换成字符串存放,也可以转换成其他的;}


简单的xml 操作 不知道是不是你想要的 

$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)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn