git push origin master
git push origin master

Reputation: 191

How to grab text between multiple occurrences of tags?

i have this description that holds users mention tag.

[MENTION=1]one[/MENTION][MENTION=4]two[/MENTION][MENTION=748]three[/MENTION] HI, it is me!!

i need to know who is the members that are mentioned in this description,so i expect.

['one','two','three']

here is what i have tried:

preg_match_all('/[MENTION=[1-9]](.*?)[\/MENTION]/s', $html, $matches);
if($matches) print_r($matches[1]);

Output:

[
  [0] => one[
  [1] =>  [
  [2] => two[
  [3] =>  [
  [4] => three[
  [5] =>  H
]

What if i need to get the numbers 1,4,748 ?

Upvotes: 1

Views: 55

Answers (2)

Jeff Puckett
Jeff Puckett

Reputation: 40861

(\d+) capturing group for 1 or more numbers

(\w+) capturing group for 1 or more word characters

as you know from [1-9] that the square brackets [] are special characters in regex that denote character sets. so if you literally want to match one of those, then you'll have to escape them with backslashes \[\]

<?php

$html = '[MENTION=1]one[/MENTION][MENTION=4]two[/MENTION][MENTION=748]three[/MENTION] HI, it is me!!';

preg_match_all('/\[MENTION=(\d+)\](\w+)\[\/MENTION\]/s', $html, $matches);

print_r($matches);
Array
(
    [0] => Array
        (
            [0] => [MENTION=1]one[/MENTION]
            [1] => [MENTION=4]two[/MENTION]
            [2] => [MENTION=748]three[/MENTION]
        )

    [1] => Array
        (
            [0] => 1
            [1] => 4
            [2] => 748
        )

    [2] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

)

Upvotes: 2

bummzack
bummzack

Reputation: 5875

Square brackets are reserved characters in a RegEx. You need to escape them. Something like this should work:

preg_match_all('/\[MENTION=([1-9]+)\](.*?)\[\/MENTION\]/s', $html, $matches);

I've also added a capture group around the IDs,([1-9]+), so you get the numeric values and the text-content in your matches ($matches[1] will contain the numbers and $matches[2] will contain the text-contents).

Upvotes: 3

Related Questions