views:

518

answers:

5

Hi,

I'm trying to write a simple php function that will strip everything between two brackets within a string.

Here is what a typical string will look like:

[img_assist|nid=332|title=|desc=|link=none|align=left|width=70|height=61] On November 14, Katherine Grove participated in Tulane University's School of Architecture Continuing Education Conference . She presented the firm's work in a presentation titled "Cradle to Cradle Design for the Built Environment".The Tulane School of Architecture Continuing Education Conference is designed to make continuing education credits available to Tulane School of Architecture alumni and the local architecture community, with a focus on sustainable design. Kathy currently directs the firm’s pro-bono involvement with the nonprofit Make It Right project in New Orleans, including assessment of all materials used in the construction of LEED Platinum certified affordable homes in the Lower 9th Ward. She oversees the firm’s residential studio, including leading the design team for a private residential project in Northern California targeted for LEED Platinum certification

I basically want to strip the "[img_assist|nid=332|title=|desc=|link=none|align=left|width=70|height=61]" that you can see at the beginning from the main body of text.

I already have a php function setup using the preg_replace function, but I can't get it to work because obviously I suck at regex.

What regex would you write to select the bracketed bit above (including the brackets)?

Thanks!

+2  A: 
preg_replace("/\[.*?\]/", "", $mystring);

matches a pair of brackets and the smallest possible amount of text between them. (this is to prevent it removing too much if there are multiple pairs of brackets - i.e. "[a]bc[d]").

Amber
+2  A: 
preg_replace('~\[.*?\]~', '', $str);
Galen
+4  A: 

Can you be 100% sure that the string inside the square brackets will never contain a right square bracket inside? If so then this simple regex should be sufficient:

preg_replace( "/\[[^\]]*\]/m", "", $string );
Salman A
+1 for a good solution if the input fits the criteria mentioned. I've written a more complicated one that allows for (escaped) square brackets. However, if nested unescaped brackets were to turn up, then regexes are not the right way to go.
Tim Pietzcker
Tim, your solution is amazing, but since I know that the sequence will never create any other characters than what you see, this one is probably all that's necessary. Thanks Salman!
bcosteloe
A: 

here's one without regex, just PHP string functions

$str = <<<A
dfsdf[img_assist|nid=332|title=|desc=|link=none|align=left|width=70|height=61] 
On November 14, Katherine Grove participated in Tulane University's School of 
Architecture Continuing Education Conference [img_assist2|nid=332|title=] 
The Tulane School of Architecture Continuing Education  Conference is designed to make 
continuing education credits available to Tulane School of Architecture alumni and the local architecture community, with a focus on sustainable design.
    A;

$s = explode("]",$str);
foreach ($s as $a=>$b){
    if ( strpos($b ,"[")!==FALSE) {
        $z = explode("[",$b);
        $b=$z[0];
    }
    print $b."\n";
}

output

$ php test.php
dfsdf
 On November 14, Katherine Grove participated in Tulane University's School of Architecture Continuing Education Conference
 The Tulane School of Architecture Continuing Education  Conference is designed to make continuing education credits available to Tulane School of Architecture alumni and the local architecture community, with a focus on sustainable design.
ghostdog74
I have to agree that regexes are not always recommended as they can become unreadable. But for such a simple case... ;)
exhuma
Nice solution...but I have to agree...regex is probably the elegant one in this case.
bcosteloe
elegant or not is subjective to each person. If it fits you, then go ahead and use regex. :)
ghostdog74
A: 

One solution with a regex that allows for escaped brackets inside the brackets:

preg_replace("/\[(?:\\\\|\\\]|[^\]])*\]/", "", $mystring);

Explanation:

\[                # match an opening bracket [
    (?:           # match one of the following...
        \\\\      # first, if possible, match an escaped backslash \\
        |         # or, if unsuccessful...
        \\\]      # match an escaped right bracket \]
        |         # or, if unsuccessful...
        [^\]]     # match any character except a closing bracket ]
    )*            # zero or more times
\]                # finally, match a closing bracket ]

This will correctly match

[img_assist|text=\[hello\]|nid=332|title=|height=61] On November 14 [test\\], Katherine Grove [hello\]] participated in Tulane University's School of Architecture Continuing Education Conference . She presented the firm's work in a presentation titled "Cradle to Cradle Design for the Built Environment".

Tim Pietzcker
Very nice! Thank you!
bcosteloe