homepage Welcome to WebmasterWorld Guest from 54.226.173.169
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Newbie Confusion with str_replace
stardoc




msg:1277971
 10:41 am on May 11, 2006 (gmt 0)

Hi all,

I am trying to edit out the words enclosed in round brackets e.g. ( widget ) from my data with the help of str_replace function.

The code I am using is:

$simple = str_replace('(WIDGET)', '', $simple);

As the words in round brackets are many, I ended up having a long series of str_replace statements in my code which have slowed down my script significantly. I wondered that there should be an easy way of doing this in PHP and searched a bit. I came across preg_replace function which is quite close to what I want but could not figure out how to write the pattern so that it can delete all the text that is enclosed in a round bracket.

I must be the oldest newbie to php as I have been playing with it for last two three years but never learnt it systematically. Any help here will be well appreciated :-)

 

Longhaired Genius




msg:1277972
 11:18 am on May 11, 2006 (gmt 0)

$simple = preg_replace('/\(.*\)/U', '', $simple);

ahmedtheking




msg:1277973
 11:45 am on May 11, 2006 (gmt 0)

Actaully, I'd try:

preg_replace("/\(([^\)]+)\)/","",$data);

This '[^\)]' means that it will get any char except for a closing bracket )

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved