Example:
Code: Select all
$string = "This is a very long [url=http://www.thisisaverylonglink.com]link[/url].";
echo wordwrap($string, 25, "",1);Code: Select all
This is a very long <a href="http://www.thisisave rylonglink.com">link</a>.Being the regexp newbie I am and not even being sure if regexp is the solution, I came running here.
Does anyone have some suggestions? I'll post my code for generating the BBCODE elements so you can see what's going on.
Code: Select all
function prepareString($string)
{
// URL
$urlPattern1 = "!\\[url=(.*?)\\](.*?)\\[/url\\]!i";
if(preg_match($urlPattern1,$string))
{
$string = preg_replace("!\\[url=(.*?)\\](.*?)\\[/url\\]!i", "<a target=\"_blank\" href=\"\\1\">\\2</a>", $string);
}
$urlPattern2 = '/\[url\](.*?)\[\/url\]/i';
if(preg_match($urlPattern2,$string))
{
$string = preg_replace('/\[url\](.*?)\[\/url\]/i','<a target="blank" href="$1">$1</a>',$string);
}
// Image
$pattern = '/\[img\](.*?)\[\/img\]/i';
$replace = '<img class="pageImage" src="$1">';
$string = preg_replace($pattern, $replace, $string);
// Setup paragraphs
$pArr = explode(chr(13),$string);
$newStr = null;
foreach($pArr as $paragraph)
{
if(trim($paragraph)=='')
{
$newStr .= '<br/>';
}
else
{
$newStr .= "<p>$paragraph</p>";
}
}
return $newStr;
}