Results 1 to 6 of 6

Thread: Split Issue

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Split Issue

    Hi guys,

    I'm having a problem with the split function.
    It doesn't work like I want it to...
    PHP Code:
    1. $msg = str_replace("[B]", '<b>', $msg);
    2.         $msg = str_replace("[/B]", '</b>', $msg);
    3.         $msg = str_replace("[U]", '<u>', $msg);
    4.         $msg = str_replace("[/U]", '</u>', $msg);
    5.         $msg = str_replace("[I]", '<i>', $msg);
    6.         $msg = str_replace("[/I]", '</i>', $msg);
    7.         $msg = str_replace('\\', '', $msg);
    8.         $urlmsg = @split('[URL="', $msg);
    9.         #$msg = $urlmsg[0];
    10.         if ($urlmsg !== false){
    11.             echo $urlmsg;
    12.             $urlmsg = split('"]', $urlmsg);
    13.             $url = $urlmsg;
    14.             $urlmsg = split('[URL="'.$url.'"]', $msg);
    15.             $urlmsg = split('[/URL]', $msg);
    16.             $msg = str_replace('[URL="'.$url.'"]'.$urlmsg.'[/URL]', '<a href="'.$url.'">'.$urlmsg.'</a>', $msg);
    17.         }
    18.         $img = @split('[IMG="', $msg);
    19.         if ($img !== false){
    20.             $imgurl = split('"]', $img);
    21.             $msg = str_replace('[IMG="'.$imgurl.'"]', '<img src="'.$imgurl.'" />', $msg);
    22.         }
    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.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  3. #3

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    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.

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    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

  5. #5

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Split Issue

    Yeah I saw that, Penagate metioned it. Thanks anyway...
    Delete it. They just clutter threads anyway.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width