Home  >  Article  >  Backend Development  >  PHP正则解决方案

PHP正则解决方案

WBOY
WBOYOriginal
2016-06-13 13:37:17755browse

PHP正则
现在要用正则取出里面 17
 


17
同步发送即时短信返回结果


我是这样写的
preg_match("/\(.*)\/U",$string,$state);

但是输出:
Array
(
  [0] => 17
  [1] => 17
)

哪些错了啊,还有我用
simplexml_load_string解析这个XML报错,本想用这个函数取这个节点,但是不行,现在只能用正则了
表帮帮忙,谢谢。

------解决方案--------------------
1.正则没错。
你可以用$state[1]获取到error节点的值。

2.simplexml_load_string()如果报错。很可能是文件BOM头的原因或者编码不一致原因。
------解决方案--------------------
你那个输出时正确的
------解决方案--------------------
你的正则没有错,你取$state[1]就行了 前面那个不用理会

你用simplexml_load_string报错 八成是xml里面的换行符 回车符导致的 把它们都替换掉

PHP code

$xml='<?xml version="1.0" encoding="UTF-8"?>
<response>
<error>17</error>
<message>同步发送即时短信返回结果</message>
</response>';

$obj=simplexml_load_string(str_replace(array("\r","\n","\t"),'',$xml));
print_r($obj);
<br><font color="#e78608">------解决方案--------------------</font><br>$xml = simplexml_load_string($s);<br>echo $xml->error;<br><br>如果出错,是因为你的编码不合适<br> <div class="clear">
                 
              
              
        
            </div>

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