Capturing Text Between Square Brackets in PHP

Capturing text between square brackets in PHP

Matches all strings with brackets:

$text = '[This] is a [test] string, [eat] my [shorts].';
preg_match_all("/\[[^\]]*\]/", $text, $matches);
var_dump($matches[0]);

If You want strings without brackets:

$text = '[This] is a [test] string, [eat] my [shorts].';
preg_match_all("/\[([^\]]*)\]/", $text, $matches);
var_dump($matches[1]);

Alternative, slower version of matching without brackets (using "*" instead of "[^]"):

$text = '[This] is a [test] string, [eat] my [shorts].';
preg_match_all("/\[(.*?)\]/", $text, $matches);
var_dump($matches[1]);

Retrieving text outside square brackets in PHP

You can extend the pattern adding \K to clean what is matched so far and then using an alternation to match 1 or more word characters.

\[[^][]+]\K|\w+

See a regex demo

$re = '/\[[^][]+]\K|\w+/';
$str = 'My [ground]name[test]Jhon[random]petor [shorts].';

preg_match_all($re, $str, $matches);
print_r(array_values(array_filter($matches[0])));

Output

Array
(
[0] => My
[1] => name
[2] => Jhon
[3] => petor
)

Capturing content between square bracket tags in PHP

You might be looking for something like this:

preg_match_all('/\[[^]]+\]([^[]+)\[\/[^]]+\]/is', $string, $matches);

This gives you the following results:

array(2) {
[0]=>
array(3) {
[0]=>
string(42) "[restab title="name"]John O'Brian[/restab]"
[1]=>
string(47) "[restab title="telephone"]+15254636544[/restab]"
[2]=>
string(62) "[restab title="address"]Newyork, Wallstreet Ave, No 5[/restab]"
}
[1]=>
array(3) {
[0]=>
string(12) "John O'Brian"
[1]=>
string(12) "+15254636544"
[2]=>
string(29) "Newyork, Wallstreet Ave, No 5"
}
}

You can add more brackets to capture text within square brackets.

Capturing text between two groups of square brackets in PHP

Maybe with this simply expression:

preg_match('/\](.*)\[/', 'test test [foo] bar [/foo] test', $match);
echo trim($match[1]);

Get all text between brackets if first bracket is missed with Regex

Assuming that nested brackets aren't to be considered you can simply use

\[?[^\]\[]*\]

or with a little less escaping

\[?[^][]*]

This will optionally match an opening square bracket, any content that doesn't contain a square bracket and the closing square bracket.

See https://regex101.com/r/97F2wM/1

(added \n to the negated character class to avoid line overlapping)

How to extract a value between square brackets in php

Use a regular expression like

preg_match_all('/\\[(.*?)\\]/', $your_string, $matches, PREG_SET_ORDER);

The array $matches[1] then will contain your values.

Get text after brackets

You can use one more capturing group in your regex pattern like this:

preg_match("/\[Ticket-ID: (\S+)\]\s*(.*)/", $mail->subject, $matches)

It will capture "Re:Test" in the second match, so you can retrieve it from $matches[2]

regex - get text between double square breackets

You need something like this

if (preg_match_all('/\[(\[(.*?)\])\]/is', $text, $matches)) {
print_r($matches[1]);
} else return "hello";

Regular expression to extract text between square brackets

You can use the following regex globally:

\[(.*?)\]

Explanation:

  • \[ : [ is a meta char and needs to be escaped if you want to match it literally.
  • (.*?) : match everything in a non-greedy way and capture it.
  • \] : ] is a meta char and needs to be escaped if you want to match it literally.


Related Topics



Leave a reply



Submit