How to find a phrase in text file print out the result PHP? -


i used code find word in .txt file:

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

and output is:

line #1 : <message id="nobill_54050898532262207218">  line #4 : <destination messageid="54050898532262207218">  line #6 : <number type="abbreviated">218</number>  line #11 : <number type="international">66830270995</number> 

but want output :

line #6 : 218 line #11 : 66830270995 

how should ?

complete text file:

<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> 

thanks.

use strip_tags....

foreach ( $lines $num => $line ) {     foreach ( $what $needle ) {         $pos = strpos ( $line, $needle );         if ( $pos !== false ) {              echo "line #<b>{$num}</b> : ". strip_tags($line);         }     }  } 

or hackish way...not recommended.

but notice full tag in $what array....

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

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -