PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
newbie2php
Forum Commoner
Posts: 35 Joined: Wed Nov 07, 2007 4:44 pm
Post
by newbie2php » Mon Aug 11, 2008 9:32 am
(Please note in this post I have put * in some of the clsoing BBcode brackets so they output on the screen as code and do not get parsed.)
Hello all.
I have a feature that allows a user to enter BBCode, such as [img]IMAGE%20PATH[/img] etc...
What I need to ensure however is that there is no spaces within the [img] tags
So
[img]
http://www.somesite.com/images/themoon.gif [*/img]
Would become
[img]
http://www.somesite.com/images/themoon.gif [*/img]
But bare in mind the whole text would get passed to me as something like this:
All in one variable.
So I need to construct a function that will search that string for the image tags, and then get rid of any spaces inbetween, and return the whole string back to me in its new perfect form.
Any ideas? I ahve been trying to use preg_replace() etc.. but have not got anywhere - I can locate the text inbetween the [img] tags with this:
$Text = preg_replace("/\[img\](.+?)\[\/img\]/", '$1', $Text);
I think... but I am unsure on how I can then strip away the whitespace and then return it to the main string.
Any help much welcome!
filippo.toso
Forum Commoner
Posts: 30 Joined: Thu Aug 07, 2008 7:18 am
Location: Italy
Contact:
Post
by filippo.toso » Mon Aug 11, 2008 9:52 am
You can use the e modifier and use the trim() function (there's an example in the PHP official manual).
newbie2php
Forum Commoner
Posts: 35 Joined: Wed Nov 07, 2007 4:44 pm
Post
by newbie2php » Mon Aug 11, 2008 10:06 am
Sorry, I do not understand.
I do not understand the e modifer?
trim() seems to just trim all white space, which is not what I want.
I want it to trim white space only between certain tags:
$string = "[img] text with spaces[*/img] this is out side of the tags so okay";
I would want returning as
$string = "[img]textwithspaces[*/img] this is out side of the tags so okay";
I am unsure if you mean to just do $stirng = trim(string); ? As that will obviosuly not work as it will trim all white space...
Thanks
filippo.toso
Forum Commoner
Posts: 30 Joined: Thu Aug 07, 2008 7:18 am
Location: Italy
Contact:
Post
by filippo.toso » Mon Aug 11, 2008 10:11 am
Please open the official PHP manual and read the
Example #4 Using the 'e' modifier :
http://www.php.net/preg_replace
Instead of strtoupper(), you have to use the suggested trim() function.