pauld
pauld

Reputation: 49

preg_match regex, find text within link

Im looking to 'preg_match' a link that looks like /dp/B0039SD7S6/blah-blah my current expression looks like...

$var = preg_match('/dp\/(.?*)\//', $output);

This doesn't output '039SD7S6'. Im assuming because the backslashes interfere with the delimiter. Help would be appreciated, Thanks.

Upvotes: 1

Views: 166

Answers (1)

fusion3k
fusion3k

Reputation: 11689

There are 3 problems with your regex:

  • yes, the slash, you have to escape it or change delimiters;
  • ungreedy option: ? must be after .*
  • preg_match syntax: result must be in parameters, not in returning value.

Change in this way:

 preg_match('/dp\/(.*?)\//', $output, $var);

Or - as I prefer - in this way:

preg_match('{dp/(.*?)/}', $output, $var);

Upvotes: 2

Related Questions