Click to See Complete Forum and Search --> : Split Issue
TheBigB
Jun 26th, 2007, 03:47 AM
Hi guys,
I'm having a problem with the split function.
It doesn't work like I want it to...
$msg = str_replace("", '<b>', $msg);
$msg = str_replace("", '</b>', $msg);
$msg = str_replace("", '<u>', $msg);
$msg = str_replace("", '</u>', $msg);
$msg = str_replace("", '<i>', $msg);
$msg = str_replace("", '</i>', $msg);
$msg = str_replace('\\', '', $msg);
$urlmsg = @split('', $urlmsg);
$url = $urlmsg;
$urlmsg = split('[URL="'.$url.'"]', $msg);
$urlmsg = split(' (', $msg);
#$msg = $urlmsg[0];
if ($urlmsg !== false){
echo $urlmsg;
$urlmsg = split(')', $msg);
$msg = str_replace(''.$urlmsg.' ('.$url.')', '<a href="'.$url.'">'.$urlmsg.'</a>', $msg);
}
$img = @split('[IMG="', $msg);
if ($img !== false){
$imgurl = split('"]', $img);
$msg = str_replace('[IMG="'.$imgurl.'"]', '<img src="'.$imgurl.'" />', $msg);
}
This code converts bbcode into html.
With slight syntax changes this would work for me in VB, but somehow PHP is a little more stubborn to my side... :(
Anyone?
Thanks in advance
penagate
Jun 26th, 2007, 03:52 AM
split take a regular expression. You need to either use explode(), which doesn't, or escape the special regex characters. Since you're not actually using regular expressions, it would be better to use explode().
There is an example of a BB code parser in the Codebank-PHP section.
TheBigB
Jun 26th, 2007, 07:37 AM
Alright, I got it to work... (to a certain extent, but i'll continue later...)
Now the problem is that it also goes parsing smileys in the url
(which is the process following)
mysql_select_db('pfm');
for ($i = 01; $i <= 23; $i += 1){
$ni = $i;
$q = "SELECT symbol FROM gb_smileys WHERE id = $ni";
$ret = mysql_query($q);
$ret = mysql_fetch_assoc($ret);
$ni = $ret['symbol'];
if ($i < 10){
$i = "0$i";
}
$msg = str_replace($ni, '<img src="smil/'.$i.'.ico" />', $msg);
}
This code recognizes each symbol in a text and replaces it with an html <img> tag.
Now the problem is that I also have the smiley -> :/
And in the URL I have "http://etc..."
I can always leave the smiley out, but I'd like to know if there's a way of skipping the url's and images.
Thanks
dclamp
Jun 26th, 2007, 02:40 PM
might want to look at this thread for the bbcode:
http://vbforums.com/showthread.php?t=386738
TheBigB
Jun 27th, 2007, 01:00 AM
Yeah I saw that, Penagate metioned it. Thanks anyway...
visualAd
Jun 27th, 2007, 05:39 PM
If you don't want to include URL's you can use a regular expression that utilises assertions to check that the string is not inside a URL. I would add an extra field to your database that flags weather the string is a normal match or a regexp.
for ($i = 01; $i <= 23; $i += 1){
$ni = $i;
$q = "SELECT symbol,reg_exp FROM gb_smileys WHERE id = $ni";
$ret = mysql_query($q);
$ret = mysql_fetch_assoc($ret);
$ni = $ret['symbol'];
if ($i < 10){
$i = "0$i";
}
if ($ret['reg_exp']) {
$msg = preg_replace($ni, '<img src="smil/'.$i.'.ico" />', $msg);
} else {
$msg = str_replace($ni, '<img src="smil/'.$i.'.ico" />', $msg);
}
}
Example of regular expression:
/^|(?<!http):\/(?!\/)|$/i
The regular expression explained:
The regular expression should be contained within delimiters. These are forward slashes. "//i" the i at the end signifies the regular expression should be case insensitive.
The first bracketed part is a look behind assertion. An assertion is matched by PHP but is not included within the matched text. (?<! signifies the start of a negative lookbehind assertion. I.e: the preceding text should not match this expression. So, (?<!http) indicates the preceding text should not be http.
The next part is the smiley to match. The / needs to be escaped as it is a regular expression character.
The third bracketed part is a negative look ahead assertion. It is identical to a look behind assertion, in that it is not included in the match, except that it looks ahead of the string. (?!\/) checks to ensure the next character is not a forward slash. /
^ and $ are special assertions. They match the beginning and the end of the string respectively and you need to include these, just in-case the user decides to use the smiley as the first are last characters of the message.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.