Search code examples
phpregexurlpattern-matchinghref

Use preg_match() to get URLs with optional trailing text


I want to retrieve following URLs with a regex:

`HREF="http://www.getty.edu/vow/TGNFullDisplay?find=&place=&nation=&english=Y&subjectid=7009830"`

Or

HREF="http://www.getty.edu/vow/TGNFullDisplay?find=&place=&nation=&english=Y&subjectid=7009830&ptype=PF"

the difference is the ending. the first one doesn't have &ptype=PF and the second one does.

At the moment, I'm using this pattern:

 protected $uriPattern = '/http:\/\/www\.getty\.edu\/vow\/.*?\?find=&place=&nation=&english=Y&subjectid=......./i';

but that works only for the first one.

I wonder how the regex pattern would look like for the preg_match_all() to match both of them.


Solution

  • If there is an optional part in the strings you are matching, you can add (optional)?, in your case (&ptype=PF)?.