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

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改