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
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));
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.