dph6308 2015-07-24 07:07
浏览 55
已采纳

尝试使用PHP在XML文件中查找字符串

I had a KML file that i've changed into an XML and i can output any information from it with no problem. But my "description" tag has a CDATA block inside which has HTML code. Im using PHP to echo something if "description" tag contains "some HTML code". But in my XML file, the "td" tags (which are in a CDATA block as i said) are separated by two line breaks and because of that my if statement doesnt work.

My PHP:

$a=array();
foreach($xml->Document->Folder->Placemark as $casa) {
    if(strpos($casa->description,'<td>Mob_Reduzi</td>**WHAT TO PUT HERE**<td>0</td>') !== false){
       echo "BLA BLA BLA"
    }
}

I've tried that PHP code with line break unicodes between those tags but nothing worked. Eliminate the line break is not an option right now because there are thousands of those lines. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • drno94939847 2015-07-24 07:25
    关注

    Depending on what type of line breaks they are (which usually depends on the OS), you can use one of the following escape sequences to represent a line break in a string:

    • for Unix/OS X
    • Windows

    Note that these are only interpreted by PHP inside double quoted strings. So with that in mind (and assuming there's always 2 line breaks like you mentioned in the question) you can change your check to this:

    Unix/OS X line breaks:

    if(strpos($casa->description,"<td>Mob_Reduzi</td> <td>0</td>") !== false){

    Windows line breaks:

    if(strpos($casa->description,"<td>Mob_Reduzi</td> <td>0</td>") !== false){

    If there are a variable/unpredictable number of line breaks, you're probably better off using regex: http://php.net/manual/en/function.preg-match.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部