ajsie
ajsie

Reputation: 79686

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.

Upvotes: 0

Views: 83

Answers (3)

Synetech
Synetech

Reputation: 9925

I was going to suggest the more succinct

"/http://www\.getty\.edu/vow/TGNFullDisplay\?find=&place=&nation=&english=Y&subjectid=.+(&ptype=PF)?/i"

The forward slashes are not special in either PHP nor RegEx, and thus do not need to be escaped, and the ID could be a different length.

Upvotes: 0

Otto Allmendinger
Otto Allmendinger

Reputation: 28268

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

Upvotes: 3

Pete McKinney
Pete McKinney

Reputation: 1211

Try this

protected $uriPattern = '/http:\/\/www\.getty\.edu\/vow\/.*?\?find=&place=&nation=&english=Y&subjectid=.......(&ptype=PF){0,1}/i';

Upvotes: 1

Related Questions