
December 15th, 2003, 04:03 PM
|
 |
A happy helper
|
|
Join Date: Nov 2003
Location: Stavanger, Norway
Posts: 458
  
Time spent in forums: 2 Days 6 h 29 m 6 sec
Reputation Power: 7
|
|
Okey, dokey, are you ready for some heavy magic? Here goes:
PHP Code:
<?php
$text = <<<END_OF_TEST
<p><a href="http.html">Humpty, </a> dumpty sat on a <a style="class"
href='class.html'>wall, <br />Humpty, dumpty, made a
<a href="http:fall.html">fall</a>, <br />And all the king horses, and all
the <a href="https://somewhere.com">kings</a> <a href='men.html'>men</a><br />
Could never put Humpty, dumpty, together again.</p>
END_OF_TEST;
$base = "http://www.nowhere.com/";
echo "<pre>". preg_replace("/</", "<", $text). "</pre>";
$text = preg_replace_callback("/<a([^>])*href=([\\"'])([^\\"']*)2>/Us",
create_function('$match',
'global $base;
if (!preg_match ("/^(https?|ftp):/", $match[3])) {
return "<a$match[1]href="$base$match[3]\\">";
} else {
return "<a$match[1]href="$match[3]\\">";
}'), $text);
echo "<pre>". preg_replace("/</", "<", $text). "</pre>";
?>
This could possibly have been written using only the e-modifier, but once you get used to the idea of introducing a function within the regexp it gives you another park to play in, so to speak. And since I wasn't sure what you wanted to do with those not matching https? or ftp I chose to add a $base to them. Change to whatever you like. But remember to escape any single quotes within the code.
Does this solve your problem?
<e><
|