Friday, 15 April 2016

php - RegEx capturing BBCode Quote username and ID



I'm looking to create some regex that will capture the username and post ID from BBCode QUOTE tags.




I only want the text to match when the username is directly followed by a post ID and is a fully formed QUOTE tag (e.g. [QUOTE=username;numericalid]



I'm currently using this:



$matches = preg_match_all('/(\[QUOTE=)(\w*?)(\;)/', $data, $match);


But this is matching with "QUOTE=Jim][QUOTE=Bob" from the text below, not just "[QUOTE=Bob".




I'm also trying to only return the username and ID, so the ideal return would be "Bob;20499344" (for every username/ID pattern in the text).



[QUOTE=Bob;204992][QUOTE=Steve;204939][QUOTE=Steve;204938][QUOTE=Steve;204936]Here is some text.

[QUOTE=Jim][QUOTE=Bob;20499344][QUOTE=Bob;203489194][QUOTE=Bob;67235994]some more quote here[/QUOTE][/QUOTE][/QUOTE][/QUOTE][/QUOTE][QUOTE=Bob;204194]

=hello;78967876]


How can I modify the regex to match this correctly?




Any help would be awesome! Thanks!


Answer



You can use the following regex and then work with group #1 to get your "ideal return":



\[QUOTE=([\w ]+;\d+)


[\w ]+ looks for a user name (consisting of word characters and spaces; you might need to alter that to your actual restrictions), \d+ looks for at least one digit.




Small sample code:



$string = "[QUOTE=jim][QUOTE=bob;20499344][QUOTE=bob;203449194][QUOTE=bob;67204994]some more quote here[/QUOTE][/QUOTE][/QUOTE][/QUOTE][/QUOTE][QUOTE=bob;204994]";
$matches = preg_match_all('/\[QUOTE=([\w ]+;\d+)/', $string, $match);
print_r($match[1]);
?>


Output:




Array
(
[0] => bob;20499344
[1] => bob;203449194
[2] => bob;67204994
[3] => bob;204994
)

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...