Jump to content

Using REGEX to harvest links in HTML


Rottingham

Recommended Posts

Hello

 

I have a replace_links() function that searches HTML source and harvests all the links into an array. I'm finishing eNewsletter software and have a few last bugs to work out.

 

My regex is:

if( !preg_match_all("/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU", $template_source, $matches, PREG_SET_ORDER ) ) {
	echo "      Failed to find any matches using regex: /<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU.<br>";
	return;
}

 

This works beautifully! I then add some code to change the link to point to a click tracker and insert it back into the HTML.

 

My dilemma is that if the link includes any white spaces the regex fails. IE if I did a mailto link like this:

 

<a href="mailto:dummy@dummer.com?subject=Thanks for visiting">Mail me</a>

 

The regex fails. If I place any character in the whitespace, like an underscore it works fine. How can I include whitespace in that regex line? I'm not very good at this stuff!

 

Thanks for any help.

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.