I have a string like this:
[url=http://www.las-vegas.cc/weddings.php:1d5lzpq4]Las Vegas Wedding[/url:1d5lzpq4]
And i need to clean it to make it valid html, like this:
<a href="http://www.las-vegas.cc/weddings.php" target="blank">Las Vegas Wedding</a>
I thought of using a regex to do the dirty work and came up with:
/\[url=(.+(?:))\](.*)\[\/url:.*\]/Ui
But this is not working properly since my results are
1 - http://www.las-vegas.cc/weddings.php:1d5lzpq4
2 - Las Vegas Wedding
How should i modify my regex to remove the part after the :
? It is the 2nd instance of :
in the string.
You can use this pattern:
~\[url=([^]]+):[^]]*](.*?)\[/url:[^]]*]~is
Note that i didn't exclude the :
from the first character class to deal with an eventual login/pass in the url and to be sure to find the last :
using the backtracking. So
$result = preg_replace('~\[url=([^]]+):[^]]*](.*?)\[/url:[^]]*]~is', '<a href="$1">$2</a>', $text);
Details of the part: ([^]]+):[^]]*]
[^]]
is the character class that matches all characters except ]
.
[^]]+
will match all characters until the closing ]
but the regex engine will go back (backtracking mechanism) until it find the :
to make the pattern succeed.
Once done, [^]]*
will match the last characters until the closing ]