0

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="../../../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
    )
Eric
  • 95,302
  • 53
  • 242
  • 374
shin
  • 31,901
  • 69
  • 184
  • 271
  • Parsing HTML with regex is painful... – Boris Guéry May 17 '10 at 19:00
  • 1
    Please use a DOM parser in conjunction with XPath to parse HTML. PHP has one, (`DOMDocument`), so forget that regex idea completely, you are wasting your time. HTML is not text, HTML is a markup language, and as such it *cannot successfully be parsed by regular expressions*. Which is repeated on this site and elsewhere probably a thousand times a day. – Tomalak May 17 '10 at 19:07
  • 2
    Take a hint from quite possibly the single most upvoted answer on stackoverflow: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – ChristopheD May 17 '10 at 19:07
  • Maybe I'm being blind but your "full out put" has images *before* headings, whereas your question has images *after* headings; or at the least, no image after "Comment Form". Which is correct? – salathe May 17 '10 at 19:14

1 Answers1

1

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 :

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
  • The PHP DOMDocument reads invalid HTML too, up to a certain extent. Everything else can be passed through tidy first to make absolutely sure. – Tomalak May 17 '10 at 19:09
  • http://php.net/manual/en/domdocument.loadhtml.php - quote: "Unlike loading XML, HTML does not have to be well-formed to load." – Tomalak May 17 '10 at 19:37