如何在文本文件中找到短语打印出结果PHP?

如何在文本文件中找到短语打印出结果PHP?,php,Php,我使用此代码在.txt文件中查找单词: <?php $lines = file('testXml.txt'); $what = array ( 'type="abbreviated">', 'type="international">'); foreach ( $lines as $num => $line ) { foreach ( $what as $needle ) { $pos = strripos (

我使用此代码在.txt文件中查找单词:

<?php
   $lines = file('testXml.txt');
   $what = array ( 'type="abbreviated">', 'type="international">');
    foreach ( $lines as $num => $line ) {
        foreach ( $what as $needle ) {
            $pos = strripos ( $line, $needle );
            if ( $pos !== false ) {
                echo "Line #<b>{$num}</b> : " . htmlspecialchars($line) . "<br />\n";
            }
        }
    } 
?>
我该怎么办

完整文本文件:

<message id="nobill_54050898532262207218"> <sms type="mo"> <retry count="0" max="0"/> <destination messageid="54050898532262207218"> <address> <number type="abbreviated">218</number> </address> </destination> <source> <address> <number type="international">66830270995</number> </address> </source> <ud type="text">,TH</ud> <scts>2013-07-02T02:34:53Z</scts> <service-id></service-id> </sms> <from>nobill</from> <to>203.146.251.229:80</to> </message>
21866830270995,TH 2013-07-02T02:34:53Z诺比尔203.146.251.229:80

谢谢。

尝试用
strip\u标签($line)
替换
htmlspecialchars($line)
。这将删除所有HTML开始和结束标记。省略内容

即使内容看起来更像XML,stip_标记函数也应该以同样的方式工作

更多信息:

使用带标签

foreach ( $lines as $num => $line ) {
    foreach ( $what as $needle ) {
        $pos = strpos ( $line, $needle );
        if ( $pos !== false ) {
             echo "Line #<b>{$num}</b> : ". strip_tags($line);
        }
    }
 }
foreach($num=>$line的行数){
foreach($针头){
$pos=strpos($line,$needle);
如果($pos!==false){
echo“Line#{$num}:”.strip_标记($Line);
}
}
}
或者另一种黑客方式…不推荐

但是请注意$what数组中的完整标记

$what = array ( '<number type="abbreviated">', '<number type="international">');

foreach ( $lines as $num => $line ) {
    foreach ( $what as $needle ) {
        $pos = strpos ( $line, $needle );
        if ( $pos !== false ) {
            $t = explode("<",str_replace($needle,"",$line));
            echo "Line #<b>{$num}</b> : ". $t[0]."<br>";
        }
    }
}
$what=array(“”,”);
foreach($num=>$line形式的行){
foreach($针头){
$pos=strpos($line,$needle);
如果($pos!==false){

$t=爆炸("这是我的.txt文件:info:218 66830270995,TH 2013-07-02T02:34:53Z nobill 203.146.251.229:80抱歉,我不能在上面添加.txt文件的信息最简单的方法-使用双重拆分…想要更多不同的解决方案吗?使用正则表达式并创建简单的语法分析,可以使用不同的解决方案n、 我真的不理解这个问题。我根据您的评论在示例文本文件上测试了您的代码,它运行良好-我只得到了第6行和第11行,正如您所需的输出。如果您不想显示XML标记,只需删除
htmlspecialchars()
并按照@webscabbler的建议使用
strip\u标记
foreach ( $lines as $num => $line ) {
    foreach ( $what as $needle ) {
        $pos = strpos ( $line, $needle );
        if ( $pos !== false ) {
             echo "Line #<b>{$num}</b> : ". strip_tags($line);
        }
    }
 }
$what = array ( '<number type="abbreviated">', '<number type="international">');

foreach ( $lines as $num => $line ) {
    foreach ( $what as $needle ) {
        $pos = strpos ( $line, $needle );
        if ( $pos !== false ) {
            $t = explode("<",str_replace($needle,"",$line));
            echo "Line #<b>{$num}</b> : ". $t[0]."<br>";
        }
    }
}