Glaanieboy
OSNN Veteran Addict
- Joined
- 6 Mar 2002
- Messages
- 2,628
Here is part of the code for my shoutbox:
(for clarification: the text is being stored in the db as raw text, so everything a user types, will be stored in the db. All of the formatting will be performed when the text is being read)
This afternoon I placed the $initial and $replace arrays together with a preg_replace() function for [URL(=bla)}bla[/URL] parsing. But then I am getting into trouble with my long words checker (in the for... loop). As you can see, the $string is first being explode()d in $parts and each part is being checked if it's a long part (longer than 30 chars), if so, it is trimmed.
So far, so good. The trouble started when I put in the checker. My long word checker cons...m/bla/bla]loooooooooooooooooooooooooooooooong are being trimmed. Then I tried putting the preg_replace after the long word checker, but that also gives trouble with the '<a href=http://www.bla.com/bla/bla>looooooooooooooooooooooooooooooooong</a>, that is also longer than 30 chars.
How can I solve this? As a temporary workaround I have set the maximum number of characters for a word to 60, but I want to have a more professional method.
Code:
$string = htmlspecialchars($fetchrow[1]);
$part = explode(" ", $string);
$ttl_words = count($part);
$smilies = array(";)" => "<img src=\"../images/smilies/wink.gif\" border=\"0\">",
"B)" => "<img src=\"../images/smilies/cool.gif\" border=\"0\">",
":)" => "<img src=\"../images/smilies/smile.gif\" border=\"0\">");
$initial = array("/\[url=(.+?)\](.+?)\[\/url\]/", "/\[url\](.+?)\[\/url\]/");
$replace = array("<a href=$1>$2</a>", "<a href=$1>$1</a>");
for ($X=1;$X<=$ttl_words;$X++)
{
$Y = $X-1;
if (strlen($part[$Y]) > 30)
{
$part[$Y] = substr($part[$Y], 0, 30)."...";
}
}
$string = implode(" ", $part);
$changebr = array("<BR>" => "<BR>", "****" => "f00k");
$string = strtr($string, $smilies);
$string = preg_replace($initial, $replace, $string);
print strtr($string, $changebr);
(for clarification: the text is being stored in the db as raw text, so everything a user types, will be stored in the db. All of the formatting will be performed when the text is being read)
This afternoon I placed the $initial and $replace arrays together with a preg_replace() function for [URL(=bla)}bla[/URL] parsing. But then I am getting into trouble with my long words checker (in the for... loop). As you can see, the $string is first being explode()d in $parts and each part is being checked if it's a long part (longer than 30 chars), if so, it is trimmed.
So far, so good. The trouble started when I put in the checker. My long word checker cons...m/bla/bla]loooooooooooooooooooooooooooooooong are being trimmed. Then I tried putting the preg_replace after the long word checker, but that also gives trouble with the '<a href=http://www.bla.com/bla/bla>looooooooooooooooooooooooooooooooong</a>, that is also longer than 30 chars.
How can I solve this? As a temporary workaround I have set the maximum number of characters for a word to 60, but I want to have a more professional method.