0

I know this question was asked many times before and was read most of them, but I have still issue with this.

I will have a string that mapped with [[[ and ]]], and I don't know the position of this string and either I don't know how many times this would be happen.

for example :

$string = '[[[this is a string]]] and this is some other part. [[[this is another]]]and etc.';

Now, would some body help me to learn how can I find this is a string and this is another

Thanks in Advance

6
  • You can use Regular expression or explode by [[[ delimiter Commented May 9, 2014 at 23:28
  • Sounds like you need a "regular expression to find and replace" Commented May 9, 2014 at 23:29
  • have you looked into preg_match_all? - php.net/manual/en/function.preg-match-all.php Commented May 9, 2014 at 23:33
  • When I exlode for [[[ I have a defective string in array... Commented May 9, 2014 at 23:35
  • preg_match_all just return integer that sais is there a match or not... Commented May 9, 2014 at 23:35

2 Answers 2

2

You need to use preg_match_all(), and you also need to be sure to escape the square brackets since they are special characters.

$string = '[[[this is a string]]] and this is some other part. [[[this is another]]]and etc.';
preg_match_all('/\[\[\[([^\]]*)\]\]\]/', $string, $matches);
print_r($matches);

Regex logic:

\[\[\[([^\]]*)\]\]\]

Regular expression visualization

Debuggex Demo

Output:

Array
(
    [0] => Array
        (
            [0] => [[[this is a string]]]
            [1] => [[[this is another]]]
        )

    [1] => Array
        (
            [0] => this is a string
            [1] => this is another
        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a method using lookbehinds and lookaheads:

$string = '[[[this is a string]]] and this is some other part. [[[this is another]]]and etc.';
preg_match_all('/(?<=\[{3}).*?(?=\]{3})/', $string, $m);

print_r($m);

This outputs the following:

Array
(
    [0] => Array
        (
            [0] => this is a string
            [1] => this is another
        )

)

Here is the explanation of the REGEX:

(?<=    \[{3}    )    .*?    (?=    \]{3}    )
  1       2      3     4      5       6      7
  1. (?<= Positive lookbehind - This combination of (?<= ... ) tells REGEX to make sure that whatever is in the parenthesis has to appear directly before whatever it is we are trying to match. It will check to see if it's there, but won't include it in the matches.
  2. \[{3} This says to look for an opening square brace '[', three times in a row {3}. The only thing is that the square brace is a special character in REGEX, so we have to escape it with a backslash \. [ becomes \[.
  3. ) Closing parenthesis ) for the lookbehind (Item #1)
  4. .*? This tells REGEX to match any character ., any number of times * until it hits the next part of our regular expression ?. In this case, the next part that it will hit will be a lookahead for three closing square braces.
  5. (?= Positive lookahead - The combination of (?= ... ) tells REGEX to make sure that whatever is in the parenthesis has to be directly in front (ahead) of what we are currently matching. It will check to see if it's there, but won't include it as part of our match.
  6. \]{3} This looks for a closing square brace ], three times in a row {3} and as with item #2, must be escaped with a backslash \.
  7. ) Closing parenthesis ) for the lookahead (Item #5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.