I have the following div:
<div class="divClass">Language:
<a href="http://www.some-site.com/something/something2/">EN</a>
<a href="http://de.some-site.com/something/something2/">DE</a>
<a href="http://es.some-site.com/something/something2/">ES</a>
<a href="http://fr.some-site.com/something/something2/">FR</a>
<a href="http://it.some-site.com/something/something2/">IT</a>
<a href="http://nl.some-site.com/something/something2/">NL</a>
<a href="http://pt.some-site.com/something/something2/">PT</a>
<a href="http://ru.some-site.com/something/something2/">RU</a>
<a href="http://gr.some-site.com/something/something2/">GR</a>
<a href="http://cn.some-site.com/something/something2/">CN</a>
<a href="http://pl.some-site.com/something/something2/">PL</a>
<a href="http://se.some-site.com/something/something2/">SE</a>
</div>
And using this regex pattern:
/<div class="divClass"><a href="(.*)">(.*)<\/a><\/div>/i
To use in the following expression:
$out=preg_replace('/<div class="divClass"><a href="(.*)">(.*)<\/a><\/div>/i',replace_link(substr('$1', strpos('$1','com/')+1),'$2'),$out);
My preg_replace returns NULL. Basically I want to get the link from within the A tag and it's value and replace the links and values with what I get from my replace_link
function.
Any ideea how to do that?
Thank you!
Your regex is not good: it matches only one <a href=...> </a>
, and you provide many.
You have to use something like:
/<div class="divClass">\(<a href="(.*)">(.*)<\/a>\)+<\/div>/i
(not sure of the non-capturing syntax in php)
And you must also deal with spaces (space char, tab, end of line) in your input. If you're sure of your input, you can use something like:
/<div class="divClass">[^<]*(<a href="(.*)">(.*)<\/a>[^<]*)+[^<]*<\/div>/i
This is how you use DomDocument: http://codepad.org/RxZ7URMB
// Create new DomDocument
$doc = new DomDocument();
$doc->loadHTML($html);
// Get all <a>
$anchors = $doc->getElementsByTagName('a');
foreach ($anchors as $a) {
echo $a->getAttribute('href') . PHP_EOL;
}
If you want to take it a step further and do your replacing: http://codepad.org/diqRQhiZ
foreach ($anchors as $a) {
$a->setAttribute('href', replace_link($a->getAttribute('href')));
}
echo $doc->saveHTML();