I hate regular expressions so much 🙁 If anyone can help me with this, it would be GREATLY appreciated.

I have a long string of html saved to the db. The following code may appear multiple times in the overall saved html:

<div class="{ELEMENT1}">
<table width="20" cellspacing="0" cellpadding="2" border="0" style="margin: 8px;">
    <tbody>
        <tr>
            <td><img width="300" height="200" src="http://www.mywebsite.com/images/{ELEMENT2}" alt="" /></td>
        </tr>
        <tr>
            <td align="right" class="articlebluesmall">{ELEMENT3}<br />
            </td>
        </tr>
<!-- This row may be optional -->
        <tr>
            <td class="articlebluesmall">{ELEMENT4}</td>
        </tr>
<!-- end optional row -->
    </tbody>
</table>
</div>

For this example, I have replaced the actual text in the html with dummy text (ELEMENTX) to indicate the pieces of the code that I want to pull out in my regex. Here's what I have so far:

$match = "/<div class=\"(.*)\">.*http:\/\/www\.mywebsite\.com\/images\/(.*)\.jpg.*class=\"articlebluesmall\">(.*)<(.*)<\/div>/";

When using it with preg_match_all, I'm getting empty values; it's clearly not finding anything. What am I doing wrong? Also, how do I handle the part of the search that is optional (i.e. sometimes it will be there and sometimes it won't)

    patterns are immensely powerful. You may want to solve this problem using DOM if your HTML is consistent.

    Here are some patterns that may help:

    // doesn't capture the 4th element, but seems to get the others quite well
    $p1 = '#<div class="([^"]+)">.*<img[^>]+src="http://www.mywebsite.com/images/([^"/]+)".*<td[^>]+class="articlebluesmall">([^<]+)<.*</div>#sU';
    
    // captures the 4th element, but 4th element is required
    $p2 = '#<div class="([^"]+)">.*<img[^>]+src="http://www.mywebsite.com/images/([^"/]+)".*<td[^>]+class="articlebluesmall">([^<]+)<.*(<td class="articlebluesmall">([^<]+)</td>).*</div>#sU';
    

    I haven't been able to get exactly what you want, but this might help.

      Could you be a little more specific on what your trying to accomplish? Are you trying to fetch the html string from the DB then replacing {ELEMENT X} with other DB results?

        Write a Reply...