Page 1 of 1

Need Help greatly - text in tags

Posted: Mon Aug 11, 2008 9:32 am
by newbie2php
(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:
The Moon![*/b]

Some text here

a few line breaks etc..

[img]http://www.somesite.com/images/themoon.gif[*/img]


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!

Re: Need Help greatly - text in tags

Posted: Mon Aug 11, 2008 9:52 am
by filippo.toso
You can use the e modifier and use the trim() function (there's an example in the PHP official manual).

Re: Need Help greatly - text in tags

Posted: Mon Aug 11, 2008 10:06 am
by newbie2php
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

Re: Need Help greatly - text in tags

Posted: Mon Aug 11, 2008 10:11 am
by filippo.toso
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.