M
M
Maxim Ivanushchik2010-12-01 01:30:16
HTML
Maxim Ivanushchik, 2010-12-01 01:30:16

Parsing consecutive tags?

It was necessary to combine img tags going each other into one block, which with the help of javascript will turn into a gallery. Adding one img to the block is easy - preg_replace will work here. But how to select several consecutive tags in a separate block?
Let's say we have this code:

<font color="black"><font color="#0000ff">&lt;</font><font color="#800000">img</font> <font color="#ff0000">src</font><font color="#0000ff">=&quot;image1.jpg&quot;</font> <font color="#ff0000">alt</font><font color="#0000ff">=&quot;&quot;</font> <font color="#0000ff">/&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">img</font> <font color="#ff0000">src</font><font color="#0000ff">=&quot;image2.jpg&quot;</font> <font color="#ff0000">alt</font><font color="#0000ff">=&quot;&quot;</font> <font color="#0000ff">/&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">img</font> <font color="#ff0000">src</font><font color="#0000ff">=&quot;image3.jpg&quot;</font> <font color="#ff0000">alt</font><font color="#0000ff">=&quot;&quot;</font> <font color="#0000ff">/&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">p</font><font color="#0000ff">&gt;</font>eewrwerr<font color="#0000ff">&lt;/</font><font color="#800000">p</font><font color="#0000ff">&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">div</font><font color="#0000ff">&gt;</font>qweqweqwe<font color="#0000ff">&lt;/</font><font color="#800000">div</font><font color="#0000ff">&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">img</font> <font color="#ff0000">src</font><font color="#0000ff">=&quot;image4.jpg&quot;</font> <font color="#ff0000">alt</font><font color="#0000ff">=&quot;&quot;</font> <font color="#0000ff">/&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">p</font><font color="#0000ff">&gt;</font>eewrwerr<font color="#0000ff">&lt;/</font><font color="#800000">p</font><font color="#0000ff">&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">img</font> <font color="#ff0000">src</font><font color="#0000ff">=&quot;image5.jpg&quot;</font> <font color="#ff0000">alt</font><font color="#0000ff">=&quot;&quot;</font> <font color="#0000ff">/&gt;</font><br/>
<font color="#0000ff">&lt;</font><font color="#800000">img</font> <font color="#ff0000">src</font><font color="#0000ff">=&quot;image6.jpg&quot;</font> <font color="#ff0000">alt</font><font color="#0000ff">=&quot;&quot;</font> <font color="#0000ff">/&gt;</font></font><br/>
<br/>
<font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>

image4.jpg just in this case is not needed, because. it is not followed by another img tag. And you need to get 2 lists of img tags: with images image1.jpg, image2.jpg, image3.jpg and image5.jpg, image6.jpg
I can't think of a regular expression to parse this code. If who met with such task, share, please, algorithm.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Y
YasonBy, 2010-12-01
@YasonBy

I'm not familiar with PHP's regexp features, but something like this:

((?:<img.*[^>]/>\s?){2,})
Your example is being tested.

S
Sannis, 2010-12-01
@Sannis

Catch a plus for perfectionism and care for users :-)

K
Konstantin Kitmanov, 2010-12-01
@k12th

DOM methods?

C
cawabanga, 2010-12-01
@cawabanga

You cannot parse [X]HTML with regexps. Because HTML cannot be parsed with regexps. Regexps are not a tool for sane HTML parsing. [...]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question