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

    Join Date
    Aug 2011
    Rep Power

    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
    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:
    $numSigLinks count($sigLinks); 
    now I'm trying to go through the sigLinks array and remove the remaining:
    PHP Code:

    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
    USA (verifiably)
    Rep Power
    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:
    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.
    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:
    Useful references:
    Spreading knowledge, one newbie at a time.

    Learn CSS. | PHP includes | HTML Validator | CSS validator

    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