今天用PHP解析xml文档 大致如下内容:
<description>
<![CDATA[<a href='http://xx.com'>a link</a>]]>
</description>用simplexml_load_string无法读到description中间的内容,各位有什么高招?我先说一下我的思路,尽管最终失败,算是抛砖引玉吧
第一步,我先讲上述字符串整理成如下形式:<description>
<a href='http://xx.com>a link'></a>
</description>
第二步我打算将<a href='http://xx.com>a link'></a>部分的“<”和">"替换为<或者>形式;如下:$str = preg_replace("/<description>(.*?)<\/description>/", search("$1"), $str);
function search($match){
$match = str_replace("<", "x", $match);
return $match;
}
但是我发现回调函数中str_replace函数无法执行;各位可有什么思路或者解决办法,谢谢;
<description>
<![CDATA[<a href='http://xx.com'>a link</a>]]>
</description>用simplexml_load_string无法读到description中间的内容,各位有什么高招?我先说一下我的思路,尽管最终失败,算是抛砖引玉吧
第一步,我先讲上述字符串整理成如下形式:<description>
<a href='http://xx.com>a link'></a>
</description>
第二步我打算将<a href='http://xx.com>a link'></a>部分的“<”和">"替换为<或者>形式;如下:$str = preg_replace("/<description>(.*?)<\/description>/", search("$1"), $str);
function search($match){
$match = str_replace("<", "x", $match);
return $match;
}
但是我发现回调函数中str_replace函数无法执行;各位可有什么思路或者解决办法,谢谢;
解决方案 »
- php判断url连接,然后给一个变量赋值
- 高分求飞信网页网站源码
- 添加内容的时候,数据中的标签问题?
- php.ini哪个参数配置,能使得$DOCUMENT_ROOT=$_SERVER['DOCUMENT_ROOT']?
- 在百度,知道上面,如果用户没有登录,点击提交问题的话,就会屏幕变灰不可用,并出现一登录窗口,如何实现
- iis7.0如何支持xdebug
- PHP中如何在跳转时将页面聚焦到一个text框? 急!
- 求 php + mssql 免ODBC分页的类
- 关于win2000+apache1.3+mysql上,如何解决发邮件的问题
- 关于一段正则表达式。。。
- PHP调试 php.ini
- 求助!十分感谢!!!!php生成静态html 分页问题
$str ="<description>
<a href='http://xx.com>a link'></a>
</description>";
function search($match){
$match = str_replace("<", "x", $match[1]);
return $match;
}
echo preg_replace_callback("/<description>(.*?)<\/description>/s", "search", $str);
//xa href='http://xx.com>a link'>x/a>
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_parse_into_struct($parser, $xmldata, $result_xml, $result_idx);
xml_parser_free($parser);
可以解析到
<?xml version="1.0"?>
<root>
<show>
<description>
<![CDATA[<a href='http://xx.com'>a link</a>]]>
</description>
</show>
</root>
XML;$xmlRoot = simplexml_load_string($xml);$ar = array();foreach($xmlRoot as $val){
$ar[] = (string) $val->description;
}
print_r($ar);
Array
(
[0] =>
<a href='http://xx.com'>a link</a>
)