检查URL是否包含某些字符串

时间:2015-02-13 07:51:14

标签: php parsing if-statement

如何检查网址($u)中是否包含embed-.html

我有这个:

function embed($u){
$servidores = array('http://powvideo.net/', 'http://gamovideo.com/', 'http://vidspot.net/');
$embedurl = array('http://powvideo.net/embed-', 'http://gamovideo.com/embed-', 'http://vidspot.net/embed-');
    $a = str_replace($servidores, $embedurl, $u);
return $a.'.html';
}

1 个答案:

答案 0 :(得分:1)

从网址中提取路径,并检查它是否以'/embed'开头:

function embed($u){
  $path = parse_url($u, PHP_URL_PATH);
  if(strpos($path, '/embed-') === 0) {
      return $u;
  }
  $servidores = array('http://powvideo.net/', 'http://gamovideo.com/', 'http://vidspot.net/');
  $embedurl = array('http://powvideo.net/embed-', 'http://gamovideo.com/embed-', 'http://vidspot.net/embed-');
  $a = str_replace($servidores, $embedurl, $u);
  return $a.'.html';
}

echo embed('http://powvideo.net/embed-n790e6gzhmmp-960x450.html') . "\n";

echo embed('http://powvideo.net/n790e6gzhmmp-960x450') . "\n";
相关问题