Jump to content

[SOLVED] Matching text from a string


ankhmor

Recommended Posts

I have a page of code.

I'm using

preg_match_all('/(<a href="){1}([a-zA-Z0-9:\/.?=]{1,})(">){1}/' ...

to find all the links.

However theres a line of code full of links which I actually need that seems to be not recognized by preg_match_all.

its starts like this

 » <a href="[link]">[name of the link]</a><br>

and just keeps repeating.

The problem is I dont get any results the preg_match_all just seems to skip that line without matching it.

 

Anyone has an idea on how to solve this.

Link to comment
https://forums.phpfreaks.com/topic/163188-solved-matching-text-from-a-string/
Share on other sites

For stuff like this, you can use DOM / XPath instead of regex:

 

Example:

$dom = new DOMDocument;
@$dom->loadHTMLFile('http://www.cs.sfu.ca/'); // replace url with site in question
$xpath = new DOMXPath($dom);
$aTag = $xpath->query('//a[@href]');

foreach ($aTag as $val) {
       echo '<span style="color: #A7AFB5">(' . $val->getAttribute('href') . ')</span> ' . $val->nodeValue . "<br /> ";
}

Archived

This topic is now archived and is closed to further replies.

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