#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    PHP Regex to remove BBCode [URL='websirte']


    Hi there,

    I'm quite new to regex and have been attempting to do this, but I'm not able to figure it out yet

    Basically want to convert
    PHP Code:
    a string [URL='anychars'sometext[/URL
    into
    PHP Code:
    a string sometext 
    I've already choped out the
    from the string, since I use it to count the number of links, and stick the result into an array:
    PHP Code:
            $sigLinksexplode("[/URL]"$signature);      
            
    $numSigLinks count($sigLinks); 
    now I'm trying to go through the sigLinks array and remove the remaining:
    PHP Code:
     [URL='whatever'

    The PHP regex that I created (but doesnt work) was the following:

    PHP Code:
    $str preg_replace('/[\[URL=\':.\/\-\d\w?\'\]]/'"",  $sigLinks[$i]); 
    But this seemed to cut out the "sometext" string at the end

    Idealy, I want to cut out the section
    PHP Code:
    [URL='[.*]'// where .* is an char of any length 
    But I havent figured out how to do this yet

    - Sorry, I'm still very new to regex and have been trying, but with very little success
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Welcome to DevShed Forums, mike224.

    1) You put your entire expression inside the brackets. That tells the engine to match the characters in any order, not just the order you want.

    You only want the brackets around the characters that will appear between the single-quotes:
    Code:
    '/\[URL=\'[:.\/\-\d\w\?]\'\]/'
    2) The question mark is a special character, so when it's not in a character class (the characters between the square brackets), it needs to be escaped with a backslash like the other special characters.

    3) You need to also put a quantifier after the character class, because by itself the character class will only match a single character. In this case you may want to use the "+" which means to match 1 or more characters.
    Code:
    '/\[URL=\'[:\.\/\-\d\w\?&=]+\'\]/'
    However, I would recommend using a negative character class instead, so you don't have to specify every character that might appear in a URL. The "^" as the first character in a character class indicates that any character not in the class will be matched, so we want to match any character that is not a single quote:
    Code:
    '/\[URL=\'[^\']+\'\]/'
    Useful references:
    http://www.php.net/manual/en/pcre.pattern.php
    http://www.regular-expressions.info/reference.html
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo