0

how to extract specific string after specific word using html dom in php. I have

<div class="abc">    
<script type="text/javascript">
var flashvars = {    word : path  }  </script>

Now i want to extract path after word

1 Answers1

0

thanks for your response. I got the solution for what i was looking.

Here is the code in case someone needs it.

Explanation : '$results' is the curl response.

Enter div class name (which you want to fetch) inside "$xpath->query() function"

You will get source code for entire class inside "$tag->textContent"

    $dom = new DOMDocument();
    $dom->loadHTML($results);
    $xpath = new DOMXPath($dom);
    $tags = $xpath->query('//div[@class="e"]');
    foreach ($tags as $tag)
   {
    echo "<br>----------<br>";
    var_dump($tag->textContent);
    echo "<br>----------<br>";
    }

Now you have your required class' html source inside "$tag->textContent".

Now you can fetch anything from the string between "start" and "end" points using below function.

function get_string_between($string, $start, $end){
   $string = ' ' . $string;
   $ini = strpos($string, $start);
   if ($ini == 0) return '';
   $ini += strlen($start);
   $len = strpos($string, $end, $ini) - $ini;
   return substr($string, $ini, $len);
}

In my case i used it like this :

$price = get_string_between($tag->textContent,'swf', '+'); echo $price;

Here "swf" is the starting point of the path and "+" is the end point. Hope it saves somebody else time :)