-
Parsing a string
How do I take text between two symbols, and do something with it, for example;
Taking these forums as an example, you can do [ url]http://www.alistairbaillie.co.uk[ /url]. (Without the sapces) and it automatically makes it a URL.
I know I could change the [ url] to <a href=" and the [ /url] to ">Click me</a>
but this doesn't give the same effect.
If anyone knows how I can achieve this, i would be gratefull.
-
-
You would however need a more advanced replace function such as ereg_replace() if you wanted [ url=www.blah.com]Text[ /url]
-
Check out this thread. Same concept.
-
how about this code:
Code:
<?php
$searcharray = array("/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/esiU",
"/(\[)(url)(])([^\"]*)(\[\/url\])/esiU");
$replacearray = array("showurl('\\5', '\\7')",
"showurl('\\4', '\\4')");
$str = "hello.net<br>
http://www.vbworld.net";
echo $str."<br><br>";
$str=preg_replace($searcharray, $replacearray, $str);
echo $str;
function showurl($url, $hyperlink) {
return "<a href=\"$url\">$hyperlink</a>";
}
?>
That will handle [url] & [url=] without trouble.
Hope that's what you want :D
-
that didn't post correctly..
quote it to get the actual code..
-
Quote:
Originally posted by da_silvy
how about this code:
Code:
<?php
$searcharray = array("/(\[)(url)(=)(['\"]?)([^\"']*)(\\4])(.*)(\[\/url\])/esiU",
"/(\[)(url)(])([^\"]*)(\[\/url\])/esiU");
$replacearray = array("showurl('\\5', '\\7')",
"showurl('\\4', '\\4')");
$str = "hello.net<br>
http://www.vbworld.net";
echo $str."<br><br>";
$str=preg_replace($searcharray, $replacearray, $str);
echo $str;
function showurl($url, $hyperlink) {
return "<a href=\"$url\">$hyperlink</a>";
}
?>
That will handle [url] & [url=] without trouble.
Hope that's what you want :D
Thanks, thats exactly what i wanted :)
-