Jump to content

Search and copy the whole line in html


durgaprasadcs

Recommended Posts

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 !

 

Link to comment
https://forums.phpfreaks.com/topic/285486-search-and-copy-the-whole-line-in-html/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.