Jump to content


This topic is now archived and is closed to further replies.


need help with preg_replace

Recommended Posts

I am trying to make a form that allows BBCODE type input

so far it works but there are a couple of problems i cant work out

one is when a link is submitted starting with www. it is converted with the following code to a showable link but I dont know how to make it work with the http:// on the beggining

[code]$url = preg_replace("/\[url=(\W?)(.*?)(\W?)\](.*?)\[\/url\]/i", '<a class="bbcode_links" href="http://$2">$4</a>', $url); // change bbcode links[/code]

I know i could str_replace it out but I cant make it work on a mix of lower and upper case letters

is there a way i can work it into the above preg_replace so it would remove the http:// at the same time as changing the code over to a html link?

my other problem is white spaces

[mytags] <<<< I have many of these tag names and need the code to work on them all so like the above code recognisses the [ ] to know its a tag, i need somthing like this for removing the white space

I want a way to remove all spaces from between [ mytags ] << them tags so i would end up with [mytags]
so even if the input was [ m y t a g s ] it would still come out as [mytags] it would also be great if i could remove new lines and breaks etc from inside them tags too.

at the same time i need to fine a way of doing cind of the same thing with the text outside the tags but not all the spaces just the leading and ending space eg
[mytags] blah blah blah [/mytags] would become [mytags]blah blah blah[/mytags] i dont want to remove new lines or anything with this part though

please help it has taken me days to work out the rest and its realy not far from finnished now this is just about my last problem

Share this post

Link to post
Share on other sites
Not sure how you would accomplish all of this, it would seem like a large number of explode and implode functions to me...

However, if you having problems with case sensitivity with str_replace(), use:

(PHP 5)
str_ireplace -- Case-insensitive version of str_replace().

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.