|
-
Jun 26th, 2007, 02:47 AM
#1
Thread Starter
Frenzied Member
Split Issue
Hi guys,
I'm having a problem with the split function.
It doesn't work like I want it to...
PHP Code:
$msg = str_replace("[B]", '<b>', $msg);
$msg = str_replace("[/B]", '</b>', $msg);
$msg = str_replace("[U]", '<u>', $msg);
$msg = str_replace("[/U]", '</u>', $msg);
$msg = str_replace("[I]", '<i>', $msg);
$msg = str_replace("[/I]", '</i>', $msg);
$msg = str_replace('\\', '', $msg);
$urlmsg = @split('[URL="', $msg);
#$msg = $urlmsg[0];
if ($urlmsg !== false){
echo $urlmsg;
$urlmsg = split('"]', $urlmsg);
$url = $urlmsg;
$urlmsg = split('[URL="'.$url.'"]', $msg);
$urlmsg = split('[/URL]', $msg);
$msg = str_replace('[URL="'.$url.'"]'.$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
Delete it. They just clutter threads anyway.
-
Jun 26th, 2007, 02:52 AM
#2
Re: Split Issue
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.
-
Jun 26th, 2007, 06:37 AM
#3
Thread Starter
Frenzied Member
Re: Split Issue
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)
PHP Code:
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
Delete it. They just clutter threads anyway.
-
Jun 26th, 2007, 01:40 PM
#4
Re: Split Issue
might want to look at this thread for the bbcode:
http://vbforums.com/showthread.php?t=386738
Last edited by dclamp; Jun 26th, 2007 at 01:45 PM.
My usual boring signature: Something
-
Jun 27th, 2007, 12:00 AM
#5
Thread Starter
Frenzied Member
Re: Split Issue
Yeah I saw that, Penagate metioned it. Thanks anyway...
Delete it. They just clutter threads anyway.
-
Jun 27th, 2007, 04:39 PM
#6
Re: Split Issue
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.
PHP Code:
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:
Code:
/^|(?<!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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|