Nakouzia Nakouzia - 6 months ago 11
PHP Question

Get only the next 6 characters after a string and loop the code to get them all

I found this link useful

Get only the next 6 characters after a word

but how to make it loop on the entire page using "foreach"

$url = 'http://www.example.com/news/page';
$needle = '<div class="title"><a href="http://www.example.com/news/';
$contents = file_get_contents($url);
$str = substr($contents, strpos($contents, $needle) + strlen($needle), 6);


this code result
123456
as the id for the first found...

How to make it loop the entire page?

Answer

I would advice against doing this with strpos, because the file may have slight variations of the HTML you are looking for, with an extra space here and there, or a line break, an extra attribute, ...etc.

The way to parse HTML is by using the DOM. In PHP this is done with the DOMDocument class:

$url = 'http://www.example.com/news/page'; 
$doc = new DOMDocument();
// ignore errors due to malformed HTML at URL
libxml_use_internal_errors(true);
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//div[@class='title']/a[starts-with(@href, 'http://www.example.com/news/')]");
$href="";
foreach ($elements as $a) {
    $href = $a->getAttribute('href');
    break; // only the first has our interest
};
// At this point $href is the full href content.
// Split the URL in parts by "/", and get the part that interests us
$num = explode("/", $href)[4]; // adapt "4" to get the right part

echo $num; // 123456