August 29th, 2011, 03:46 AM
PHP Regex to remove BBCode [URL='websirte']
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
a string [URL='anychars'] sometext[/URL]
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:
a string sometext
now I'm trying to go through the sigLinks array and remove the remaining:
$sigLinks= explode("[/URL]", $signature);
$numSigLinks = count($sigLinks);
The PHP regex that I created (but doesnt work) was the following:
But this seemed to cut out the "sometext" string at the end
$str = preg_replace('/[\[URL=\':.\/\-\d\w?\'\]]/', "", $sigLinks[$i]);
Idealy, I want to cut out the section
But I havent figured out how to do this yet
[URL='[.*]'] // where .* is an char of any length
- Sorry, I'm still very new to regex and have been trying, but with very little success
August 29th, 2011, 06:25 AM
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: