Doug Fir
Doug Fir

Reputation: 21204

If statement based on url contains

I know this is pretty basic to those who follow the php tag but I'm a learner working on my first plugin.

I'm generating meta tags based on what page the visitor is viewing.

Using php, how do I say

IF url contains "^/reports/view/*" then do this(),
Else do that()

I'm not asking about syntax of the IF statement I can figure that out. It's using a regular expression in php. The particular set of pages I'm trying to target start with /reports/view/ and then have something after.

Here are some example pages I wish to target:

example.com/reports/view/somepage.php
example.com/reports/view/someotherpage.php

I've used if == before but never contains in this sense.

Upvotes: 0

Views: 325

Answers (1)

raina77ow
raina77ow

Reputation: 106385

There seems to be no need for employing regex in this case: you can use strpos() method just fine:

if (strpos('/reports/view/', $url) === 0) {
  // $url begins with '/reports/view/'
}

if (strpos('/reports/view/', $url) !== FALSE) {
  // $url contains '/reports/view/' substring
}

Upvotes: 1

Related Questions