Wordwrap + possible regexp help
Posted: Mon May 15, 2006 2:39 pm
Lately I've been developing a forum for some friends, and have implemented some simple bbcode support for the classics bold, italic, underline, image and urls. When it came to splitting long words to prevent them from messing up the layout, I thought wordwrap would be enough, but I didn't think about the bbcode.
Example:
Would output:
Causing the link to break. In some cases it would insert a whitespace into the [/url] element, which would break the regexp generating the links.
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.
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;
}