warlord
03-14-2007, 01:42 AM
I'm trying to parse/split the following text using preg_replace and haven't had too much luck so far ... if anyone could shed some light on how to get the results I would really appreciate it.
String =
string is surrounded by other text and also
contains the brackets exactly as shown ... [[please parse|this part]]
I would like to do something like this with it:
<a href="http://www.domain.com/please parse">this part</a>
Thanks ~
mlseim
03-14-2007, 02:27 PM
Here you go .....
<?php
$text="string is surrounded by other text and also contains the brackets exactly as shown ... [[please_parse|this part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[also_parse|this second part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[another_part|to parse]]
";
function url_links($string)
{
$string = preg_replace('/\[\[(.*?)\|(.*?)\]\]/i', '<a href="http://www.mysite.com/$1" target="_blank">$2</a>', $string);
return $string;
}
print url_links($text);
?>
warlord
03-15-2007, 02:32 AM
Here you go .....
<?php
$text="string is surrounded by other text and also contains the brackets exactly as shown ... [[please_parse|this part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[also_parse|this second part]] string is surrounded by other text and also contains the brackets exactly as shown ... [[another_part|to parse]]
";
function url_links($string)
{
$string = preg_replace('/\[\[(.*?)\|(.*?)\]\]/i', '<a href="http://www.mysite.com/$1" target="_blank">$2</a>', $string);
return $string;
}
print url_links($text);
?>
mlseim, it works great ... although I forgot to mention I also have a few in the string such as [[hyperlink]] that don't have the '|' in them in addition to the others and need them to be like:
<a href="http://www.mysite.com/hyperlink" target="_blank">hyperlink</a>
I tried adding the below code, although it didn't seem to work and the output gets jumbled up.
<?php
$text="string is surrounded by other text and also contains the brackets exactly as shown ... [[please_parse|this part]] string is [[surrounded]] by other text and also contains the brackets exactly as shown ... [[also_parse|this second part]] string is surrounded by other text and also [[contains]] the brackets exactly as shown ... [[another_part|to parse]]
";
function url_links($string)
{
$string = preg_replace('/\[\[(.*?)\|(.*?)\]\]/i', '<a href="http://www.mysite.com/$1" target="_blank">$2</a>', $string);
$string = preg_replace('/\[\[(.*?)\]\]/i', '<a href="http://www.mysite.com/$1" target="_blank">$1</a>', $string); // Added
return $string;
}
print url_links($text);
?>
warlord
03-15-2007, 05:24 AM
... got it figured it out, although I'm not sure if it's the most effective way ...
:)