-2

I have a html content which has a URL fragment looks like following:

<a href="#/news/0294928/what-a-great-idea">

What I need to do is just to grab the number followed by #/news/

Any good idea? I tried following, but it gets only 4 results for me

preg_match_all('/<a href="#\/news\/(.+)\/?\"?>/i', $html_cont, $matches );

Please help me, Thanks.

Wilson Breiner
  • 155
  • 1
  • 2
  • 9

2 Answers2

0

The detail of my comment:

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($yourHTML);

$xp = new DOMXPath($dom);
$hrefNodeList = $xp->query('//a/@href[starts-with(., "#/news/")]');

foreach ($hrefNodeList as $hrefNode) {
    echo explode('/', $hrefNode->nodeValue)[2] . PHP_EOL;
}
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0
$string = '<a href="#/news/0294928/what-a-great-idea">';
preg_match_all("/#\/news\/(.*?)\//",$string, $m);
echo '<pre>'.print_r($m[1]).'</pre>';

so in $m[1] are all your numbers if thats what ure lookin for?

Donny Doe
  • 46
  • 7