B
B
Big_person2015-11-20 12:20:16
PHP
Big_person, 2015-11-20 12:20:16

How to get the value in a string in square brackets using a regular expression?

There is a line like "some text [any number]"
How to get the value in square brackets in php, along with the square brackets themselves?
That is, from the string "some text [any number]" you need to get "[any number]".
To be completely accurate, I need to get a string without everything that is in square brackets, leaving only "some text"
in php, for this you can use
preg_replace($pattern, "", "text[3]"); // text
Here's what to specify in $pattern?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2015-11-20
@Big_person

https://regex101.com/r/iN7rY2/2

$str = 'некий текст[36465464]'; 

echo $str, PHP_EOL;
echo preg_replace('/\[\d+\]/', '', $str);
ideone.com/OOAxcK

O
Optimus, 2015-11-20
Pyan @marrk2

preg_replace("/\[(.*?)\]/ism", "", "text[3]");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question