Aaran McGuire
Aaran McGuire

Reputation: 3225

Getting links with certain anchor text using regex

I'm looking to do a preg_match_all to catch the href of a link that has the anchor text of free file.

This is what I have so far <a.+href="(.*)".*>free file</a> but its not working as I'm getting a error of...

Warning: preg_match() [function.preg-match]: Unknown modifier 'f' in /home/aaran/public_html/tests/free_file.php on line 20

PS I'm wanting to match the URL from this:

<p> Grab this month's <a href="/item/html5-image-transitions-jquery-plugin/571431?WT.ac=free_file&amp;WT.seg_1=free_file&amp;WT.z_author=pixelentity">free
file</a> from the Canvas category! </p>

Upvotes: 1

Views: 740

Answers (1)

Ry-
Ry-

Reputation: 224913

The problem appears to be that you didn't put delimiters around your expression. PHP requires the expression to be surrounded by some form of valid delimiter. Common ones are:

  • /, usually used unless there's a particular reason not to do so
  • ~
  • #

So make sure your preg_match starts like this:

preg_match('~<a.+href="(.*)".*>free file</a>~', /* ... */

Note that I used the tilde as a delimiter because you have a / in your expression.

Upvotes: 3

Related Questions