Leeroy
Leeroy

Reputation: 5

preg_match: check if it doesn't includes a word

Code:

if (!preg_match("https://www.youtube.com/channel/~i", $l1)) {
    $l1 = $l1 . "?sub_confirmation=1";
  }

My query is, if a link doesn't contain a ?sub_confirmation=1, it is going to be added. But if a link already contains it, it shouldn't be added! How to do that ?

Upvotes: 0

Views: 588

Answers (3)

shubham715
shubham715

Reputation: 3302

You can use strpos()

Example -

  $link = "https://www.youtube.com/channel/~i";
  $search = '?sub_confirmation=1';
  if (strpos($link, $search) === false) {
   $link =  $link.$search;
  }
  echo $link;

EDIT

if you want only for specific url (in your case only for youtube channel urls) then add one more if condition like example below.

Try this

 $link = "youtube.com/watch?v=example";
  $search = '?sub_confirmation=1';
  $searchUrl = "youtube.com/channel";
  if (strpos($link, $searchUrl) !== false && strpos($link, $search) === false) {
   $link =  $link."".$search;
}
echo $link;

Upvotes: 2

MikeSouto
MikeSouto

Reputation: 240

Try this:

if( ! preg_match('#sub_confirmation=1#is', $l1 ) )
{
    $l1 = $l1 . "?sub_confirmation=1";
}

even so, i recommended you play with parse_url.

Upvotes: 1

Fotis Grigorakis
Fotis Grigorakis

Reputation: 361

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];


if (strpos($url,'YourWord') !== false) {
    echo 'Word exists.';
    //your code for not adding the link
} else {
    echo 'Word doesn't exist.';
    //your code for adding the link
}

Upvotes: 0

Related Questions