Php, regex and preg_replace need some help

I need help with a little replacement:

Some text [ id ] 

to ...

 Some text | id 

I'm new to regex and I just don't know how to safely store text inside [] ... And I don't want to use str_replace and crop ... I need to use expressions (don ask why: D) ... Can anyone Anything to help me?

+4
source share
2 answers

This should work for non-nested square brackets:

 preg_replace("/\[(.*?)\]/", "|$1", "Some text [ id ]") 

OUTPUT

 Some text | id 
+5
source

You do not need a regular expression for such a simple task, str_string () will do this.

 $str = str_replace(array("[","]"), array("|", ""), $str); 

OUTPUT

 Some text | id 

Using a regex to do something like this is like asking Einstein to solve 2 + 2.

0
source

All Articles