Nakouzia Nakouzia - 10 months ago 25
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 = '';
$needle = '<div class="title"><a href="';
$contents = file_get_contents($url);
$str = substr($contents, strpos($contents, $needle) + strlen($needle), 6);

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

How to make it loop the entire page?


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 = ''; 
$doc = new DOMDocument();
// ignore errors due to malformed HTML at URL
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//div[@class='title']/a[starts-with(@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