Jump to content

Need help w/ regular expressions


yacaph

Recommended Posts

Hi,

 

I'm having problems understanding how to create regular expressions. I need to convert a portion of a newsletter that is retrieved from a MySQL table to plain text for a mailing. This example is for one item in this portion of the newsletter:

 

<span style="font-weight: bold;">New:  Must See </span>

<br style="font-weight: bold;" /><span style="font-weight: bold;">DVD Movie</span><br style="font-weight: bold;" />

<a style="font-weight: bold;" href="http://www.somesite.com/view_trailer.asp" target="_self">View Movie Trailer</a>

<br style="font-weight: bold;" /><a style="font-weight: bold;" href="http://www.somesite.php" target="_self">Purchase for Just $15</a>

 

Most of the time the items contained in this portion of the newsletter are similar to the formatting used in the example.

 

I need to be able to keep each item's text and links together so that the plain text will look something like:

 

New:  Must See DVD Movie

View Movie Trailer http://www.somesite.com/view_trailer.php

Purchase for Just $15 http://www.somesite.php

 

I found a regular expression for the links (which works but I don't understand it):

$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";

 

which returns the following in an array but, by itself it's not very usable as the only text I can retrieve is $matches[3] & I really need to get the text from the <span> tags too ;

    [2] => Array

        (

            [0] => <a style="font-weight: bold;" href="http://www.somesite.com/view_trailer.asp" target="_self">View Movie Trailer</a>

            [1] => "

            [2] => http://www.somesite.com/view_trailer.php

            [3] => View Movie Trailer

        )

 

    [3] => Array

        (

            [0] => <a style="font-weight: bold;" href="http://www.somesite.com/products/dvd_new.php" target="_self">Purchase for Just $15</a>

            [1] => "

            [2] => http://www.isomesite/dvd_new.php

            [3] => Purchase for Just $15

 

Is this even doable with regular expressions, or is there a better way?

 

Any and all help will be greatly appreciated,

 

Joseph

 

Link to comment
Share on other sites

is there a better way?

 

The better way would be to not store formatted info in your database in the first place.  Database data should be as raw and unformatted as possible, so that you don't have issues like this come up, when you are wanting to use the info for something else that requires a different format.

 

 

For the stuff between the span tags

 

$regexp = "<span[^>]*>(.*?)<\/span>";

 

 

 

Link to comment
Share on other sites

Thanks for the response.

 

Although I agree with you assessment of the data storage situation, in this case I've inherited a legacy application, and those type of changes at this point in time would be extensive & not cost effective.

 

And thanks for the span expression.

 

Joseph

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.