开发者

How do I rearrange HTML with PHP or regex?

I have different outputs of $slider['image']. Two examples are as follows.

The first one has <p> and </p> at the beginning and at the end. In the second one, all <img> tags have <p> and </p> tags.

One

<p>
    <span>Homepage</span><img src="../../assets/images/prints/print2_600x300.gif" alt="" width="600" height="300">
    <span>Content Page</span><img src=".开发者_C百科./../../assets/images/support_images/imageA_600x300.gif" alt="" width="600" height="300">
    <span>Dropdown Menu</span><img src="../../../assets/images/support_images/imageB_600x300.gif" alt="" width="600" height="300">
    <span>Comments List</span><img src="../../../assets/images/support_images/imageC_600x300.gif" alt="" width="600" height="300">
    <span>Comment Form</span><img src="../../../assets/images/support_images/imageD_600x300.gif" alt="" width="600" height="300">
</p>

Two

<p><span>Homepage</span></p>     
<p><img src="../../assets/images/prints/print2_600x300.gif" alt="" width="600" height="300"></p>
<p><span>Content Page</span></p>  
<p><img src="../../../assets/images/support_images/imageA_600x300.gif" alt="" 
width="600" height="300">
<p><span>Dropdown Menu</span></p>
<p><img src="../../../assets/images/support_images/imageB_600x300.gif" alt="" width="600" height="300"></p>
<p><span>Comments List</span></p>
<p><img src="../../../assets/images/support_images/imageC_600x300.gif" alt="" width="600" height="300"></p>
<p><span>Comment Form</span></p>
<p><img src="../../../assets/images/support_images/imageD_600x300.gif" alt="" width="600" height="300"

I need to change this to the following.

<li><span>Homepage</span><img alt="" src="assets/images/prints/print2_600x300.gif" /></li> 
<li><span>Content Page</span><img alt="" src="assets/images/support_images/imageA_600x300.gif" /></li> 
<li><span>Dropdown Menu</span><img alt="" src="assets/images/support_images/imageB_600x300.gif" /></li> 
<li><span>Comments List</span><img alt="" src="assets/images/support_images/imageC_600x300.gif" /></li> 
<li><span>Comment Form</span><img alt="" src="assets/images/support_images/imageD_600x300.gif" /></li>

I assume I may need regex and php (explode, str_replace, foreach) for this but I need some help.

Full output:

[0] =>; Array
    (
        [id] => 12
        [name] => Print 2
        [shortdesc] => <p>Print 2 short description</p>
        [longdesc] => <p>Print 2 long description</p>

        [thumbnail] => <p><img src="../../assets/images/prints/thumbnails/print2_223x112.gif" alt="" width="223" height="112"></p>
        [image] => <p><img src="../../assets/images/prints/print2_600x300.gif" alt="" width="600" height="300">Homepage<img src="../../../assets/images/support_images/imageA_600x300.gif" alt="" width="600" height="300">Content Page<img src="../../../assets/images/support_images/imageB_600x300.gif" alt="" width="600" height="300">Dropdown Menu<img src="../../../assets/images/support_images/imageC_600x300.gif" alt="" width="600" height="300">Comment List<img src="../../../assets/images/support_images/imageD_600x300.gif" alt="" width="600" height="300">Comment Form</p>
        [product_order] => 0
        [class] =>
        [grouping] =>
        [status] => active
        [category_id] => 5
        [featured] => front
        [other_feature] => none
        [price] => 0.00
    )


Expect for very simple tasks, it won't be easy to do what you want with regex.

You can use an XML parser like SAX or DOM.

You can also take a look to these two library :

  • phpHTMLParser
  • PHP Simple HTML DOM Parser
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜