Forum Moderators: coopster
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
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
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);