Forum Moderators: coopster

Message Too Old, No Replies

preg replace help

replacing a string within brackets to be null

         

auwagner

4:03 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



I apologize if this is easy stuff, i'm just pulling my hair out as I can't get my desired result.

Within a string, I would like to null out characters within brackets.

Example
Before preg_replace:
How are you doing?[kaltura-widget wid="5245245" width="260" height="252" addpermission="2" editpermission="2" /]Are you having a good day?

Desired Result:
How are you doing? Are you having a good day?

whoisgregg

4:31 pm on Apr 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, auwagner!

Could you post what you've tried so far? :)

auwagner

4:44 pm on Apr 3, 2009 (gmt 0)

10+ Year Member



Thanks whoisgregg!

I'm a newbie, but i tried the below just to see if i could bold in between the brackets (and eventually remove the text)

$removestr = 'How are you doing?[kaltura-widget wid="5245245" width="260" height="252" addpermission="2" editpermission="2" /]Are you having a good day?';

preg_replace('/\[([^]]+)\]/', '<b>\\1</b>', $removestr);

it didn't work. i know its not what i want, i was just trying to get there by baby steps. :) i tried other numerous things similar to this as well, but they didn't work as well. the doc for preg_replace is a bit confusing. :)

auwagner

7:43 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Sorry to bother, but any thoughts at all?

rob7591

7:52 pm on Apr 8, 2009 (gmt 0)

10+ Year Member



Does every [ end with /] ? or does it just have to be between []'s

try preg_replace('/\[.+\]/', '', $removestr)

rocknbil

10:14 pm on Apr 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$removestr = 'How are you doing?[kaltura-widget wid="5245245" width="260" height="252" addpermission="2" editpermission="2" /]Are you having a good day?';

preg_replace('/\[([^]]+)\]/', '<b>\\1</b>', $removestr);

So if I get you right, everything between [... and ] . . . . is intended to create a bold tag around the text following? This is not ideal, as it can go on multiple lines, or overlap other tags, so you really need a closing marker. But let's examine what you've done:

'/ - start match

\[ - begins with [ anywhere in the string

( - begin "saving" in $1

[^]]+ - one or more characters NOT a ] (note: . is "any character," which would also capture the closing ], so this is probably the best approach. This will also capture a closing /, whether or not it's present.)

) end "saving" the pattern in $1

\] - pattern ends with

/' end match

I think you're almost there. You would have seen the effect if instead of this:

'<b>\\1</b>'

You had done this:

"<b>$1<\/b>"

$1 will contain what you saved in (), so you're actually CAPTURING the stuff in []. You probably want this:

header("content-type:text/html\n\n");

$removestr = 'How are you doing?[kaltura-widget wid="5245245" width="260" height="252" addpermission="2" editpermission="2" /]Are you having a good day?';

$new_string = preg_replace('/([^[]+)\[[^]]+\]([^[]+)/g', "<b>$1 $2<\/b>", $removestr);

echo " new" $new_string";

Untested; let us know if it works (or not! :-) )I've added the g modifier, which means apply globally, and you may need the m modifier to treat the string as multiple lines, both for multiple instances of the match; however, if it's multiple lines or multiple instances, see previous comment about a closing marker. This will probably not work if you have multiple instances on a line, or if it spans multiple lines; you may need a closing marker for that.

Examined:

'/ - start pattern

( - Begin pattern, start capturing in $1

[^[]+ - one or more of any character NOT a [

) - End capturing in $1

\[ - followed by [

[^]]+ - followed by one or more characters NOT a ]

\] - followed by a ]

( - begin capturing pattern in $2

[^[]+ followed by one or more characters NOT a [

) - end capturing in $1

/ - end pattern

g = apply globally

EDIT: oops, misread the intent, fixed.