Forum Moderators: coopster

Message Too Old, No Replies

Need help editing a text variable...

         

wfernley

9:32 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi everyone,

I need some help with editing a text variable. Basically I have a variable:

$text = "[color=blue:fd2fa54b07]Hi everyone![/color:fd2fa54b07] more text goes here...";

What I need is the variable to not print the text in the []'s. Is there a way I can edit the text variable to take out the [] tags? The values will be different everytime so thats where it gets tough. So I was thinking is there a way I can make a script that will examine every character and look for a [ and when its found it will delete the [ as well as every character until it hits the ] and then deletes that character. So if I had a line saying "[color=blue:fd2fa54b07]Hi everyone![/color:fd2fa54b07]" It would delete the "[color=blue:fd2fa54b07]" and keep the "Hi everyone!" and then delete the "[/color:fd2fa54b07]".

Is there a way I can do this?

Thanks for your help :)

Wes

jamie

10:00 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi wes,

the more your work with php the more you'll find you need to know regular expressions. it is really time well spent! google for regular expressions or visit a site like sitepoint or devshed, they'll have lots of tutorials.

in your case you would need the ^ operator. ^] means any character except ]

hope that helps

wfernley

1:48 pm on Mar 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jamie,

thanks for the reply.

I am still a bit confused with what I should look for. Where would I use the ^ character? I need to take out the text between the [ and ] also.

Is there a special variable that will do this?

thanks again! :)

Wes

gliff

2:24 pm on Mar 22, 2005 (gmt 0)

10+ Year Member



Regular Expresions are a way of defining complex text patterns, such as "the first 10 occurrences of the letter a" or "all the text in-between brackets". If you're going to be doing any kind of PHP Programming in the future you ought to spend some time reading up on them.

The preg_replace function is just like the str_replace function, except it uses regular expresions. The following line should do what you want.


$text = preg_replace('/\[.+?\]/','',$text);

wfernley

2:34 pm on Mar 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Gliff.

Yeah I was unclear of what regular expressions are. I know I have used them before but it was mostly for basic functions.

I will have to read up on them though.

Thanks again!

Wes