dph6308 2015-07-24 15: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 15: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

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

报告相同问题?