Leeroy Leeroy - 2 years ago 65
PHP Question

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 ?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download