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

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

报告相同问题?

悬赏问题

  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题