Forum Moderators: coopster

Message Too Old, No Replies

Regex to close IMG tags?

         

cannibal bill

12:09 pm on May 8, 2008 (gmt 0)

10+ Year Member



Hi,
I saw a couple of old posts about using regex to close various tags, but I could not find an answer to this question.

I use FCKeditor to allow users to input text that is transformed into HTML.
For images, it creates (I guess via the js

createElement
)
<img>
tags instead of the
<img />
needed for W3C XHTML validation.
I already have a php function that transforms
alt=""
into
alt="image"
or
<br>
into
<br />
, and I want to enrich it like this:

1- search the string for

<img

2- save somewhere
whatever
is between this
<img
and the first following
>

3- replace, if the first-following
>
isn't preceeded by a
/
,
<img WHATEVER>
with
<img WHATEVER />

Obviously I'm no regex guru, and although I know it's important and interesting to learn, I don't really have time now.
My main problem is part 3 : do the replacement if following

>
isn't preceeded by
/

Anyway I'm sure someone has it ready for php. Thanks!

ps: I learned from previous posts that these 3 steps would fail if the

alt
tags contains
>
, but
alt
texts could also be cleaned with regex. Do you think of any other caveat?

penders

1:39 pm on May 8, 2008 (gmt 0)

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



I've not used FCKeditor, but I notice in its advertised features...
Outputs XHTML 1.0

And the online demo does seem to output self closing img tags... <img />

coopster

1:44 pm on May 8, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, cannibal bill.

And if an update to your editor doesn't work for you, you may want to have a look at PHP's PCRE [php.net] functions and what is called negative lookbehind assertions.

cannibal bill

2:29 pm on May 8, 2008 (gmt 0)

10+ Year Member



Thank you guys,

cooper : thanks for you input, I will check that when time for regex will come... Hope I'll have the time soon to read "mastering regular expressions" as they seem really useful. Basic regex is quite greek to me, so imagine "negative lookbehind assertions".....

penders : you're right, the demo produces self closing img tags... #*$!?! After checking their roadmap, it seems the version I got was a buggy beta one, previous and actual do output correct xhtml.

Thanks again !