4
$string1 = "This is test [example]";
$string2 = "This is test [example][2]";
$string3 = "This [is] test [example][3]";

How can I get the following result?

For $string1 -> example
For $string2 -> example*2
For $string3 -> is*example*3
1
  • In any language, loop through the characters if you encounter [ set flag and grab everything until ] and unset flag :)
    – Jashwant
    Jun 8, 2012 at 12:47

2 Answers 2

7
preg_match_all('/\[([^\]]+)\]/', $str, $matches);

php > preg_match_all('/\[([^\]]+)\]/', 'This [is] test [example][3]', $matches);
php > print_r($matches);
Array
(
    [0] => Array
        (
            [0] => [is]
            [1] => [example]
            [2] => [3]
        )

    [1] => Array
        (
            [0] => is
            [1] => example
            [2] => 3
        )

)

And here's the explanation for the rregex:

\[ # literal [
( # group start
    [^\]]+ # one or more non-] characters
) # group end
\] # literal ]
1
  • Can you explain the regex please ? As far as I get it. / to start regex expression. ` to escape [. [` to include set of characters ? Right ? Can you explain the full regex ?
    – Jashwant
    Jun 8, 2012 at 12:50
5

For those wary of regex, here's a solution sans that crazy regex syntax. :-) It used to really irritate me that something like this wasn't native to PHP's string functions so I built one...

// Grabs the text between two identifying substrings in a string. If $Echo, it will output verbose feedback.
function BetweenString($InputString, $StartStr, $EndStr=0, $StartLoc=0, $Echo=0) {
    if (!is_string($InputString)) { if ($Echo) { echo "<p>html_tools.php BetweenString() FAILED. \$InputString is not a string.</p>\n"; } return; }
    if (($StartLoc = strpos($InputString, $StartStr, $StartLoc)) === false) { if ($Echo) { echo "<p>html_tools.php BetweenString() FAILED. Could not find \$StartStr '{$StartStr}' within \$InputString |{$InputString}| starting from \$StartLoc ({$StartLoc}).</p>\n"; } return; }
    $StartLoc += strlen($StartStr);
    if (!$EndStr) { $EndStr = $StartStr; }
    if (!$EndLoc = strpos($InputString, $EndStr, $StartLoc)) { if ($Echo) { echo "<p>html_tools.php BetweenString() FAILED. Could not find \$EndStr '{$EndStr}' within \$InputString |{$InputString}| starting from \$StartLoc ({$StartLoc}).</p>\n"; } return; }
    $BetweenString = substr($InputString, $StartLoc, ($EndLoc-$StartLoc));
    if ($Echo) { echo "<p>html_tools.php BetweenString() Returning |'{$BetweenString}'| as found between \$StartLoc ({$StartLoc}) and \$EndLoc ({$EndLoc}).</p>\n"; }
    return $BetweenString; 
}

Of course this can be condensed quite a bit. To save someone else the effort of cleaning it up:

// Grabs the text between two identifying substrings in a string.
function BetweenStr($InputString, $StartStr, $EndStr=0, $StartLoc=0) {
    if (($StartLoc = strpos($InputString, $StartStr, $StartLoc)) === false) { return; }
    $StartLoc += strlen($StartStr);
    if (!$EndStr) { $EndStr = $StartStr; }
    if (!$EndLoc = strpos($InputString, $EndStr, $StartLoc)) { return; }
    return substr($InputString, $StartLoc, ($EndLoc-$StartLoc));
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.