tags:

views:

293

answers:

1

Anybody have a regular expression to replace the following code:

<a href="originalLink">hi</a>

with:

<a href="newLink">hi</a>
+4  A: 

PHP Simple HTML Dom Parser example:

// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');

// Find all links 
$anchors = $html->find('a');

$count = count($anchors);

for($i=0;$i<$count;$i++) {
    $anchors[$i]->href = 'someLink.html';
}

If you know the href of the anchor you want to replace, do something like:

$html->find('a[href=something]', 0)->href = 'someLink.php';
karim79
Ditto - this will be far more reliable than parsing HTML with a regex. The grammar of HTML is simply too complex and too loosely enforced for real-world regular expressions to handle all cases.
TrueWill
Is file_get_html a real method? I can't find it anywhere. What type of object does it return?
RD
I believe it comes from simple html :-)
The Pixel Developer