durgaprasadcs Posted January 19, 2014 Share Posted January 19, 2014 Hello, I have 100 of html pages locally stored in a spefic format. I am trying to extract a line from that html pages using php. I can able to search the string and i can able to determine whether the search string is found or not. so far i achieved is <?php $url = 'mylocalpage.html'; $searchstring= 'itemtype'; $contents = file_get_contents($url); if(strpos($contents, $searchstring)!== false) { echo 'Item found'; } else { echo 'Item not found'; } ?> _____________________ Example mylocalpage.html <html> <body> <script> var itemtype = "Mobile"; </script> . . . ..... </body> </html> The next step i need is if it is found i need to copy the entire line from that page. That is if itemtype found I need to copy the entire line that is (var itemtype = "Mobile";) any help appreciated ! Quote Link to comment Share on other sites More sharing options...
Barand Posted January 19, 2014 Share Posted January 19, 2014 If you are after a specific line use file instead of file_get_contents. This will give you an array of lines in the file. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 19, 2014 Share Posted January 19, 2014 (edited) If all .html files are in the same folder you can use glob to loop over them and then extract the line you are after from them. $itemtype = array(); // loop over all .html files foreach(glob('*.html') as $file) { // find and extract the value from itemtype variable if(preg_match('~var itemtype = "([^"]+)";~', file_get_contents($file), $matches)) { // add value to itemtype array. $itemtype[] = $matches[1]; } } printf('<pre>%s</pre>', print_r($itemtype, true)); Edited January 19, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.