如何使用 strpos() 而不是 preg_match()


How to use strpos() insted of preg_match()

我有一个脚本,当冲浪者来自某些网站时,它包含文件,它看起来像这样:

<?php
$referral = $_SERVER['HTTP_REFERER'];
if (preg_match('/yahoo.com'/main.html|yahoo.com'/secound.html/', $referral)) {
require_once ('a.php');
} else if (preg_match('/google.com/', $referral)) {
require_once ('b.php');
} else {
require_once ('c.php');
}
?>

但它正在杀死我的服务器,我想用 strops(( 替换它,但我不知道怎么做,我试过这个:

<?php
$referral = $_SERVER['HTTP_REFERER'];
if (strops('/yahoo.com'/main.html|yahoo.com'/secound.html/', $referral)) {
require_once ('a.php');
} else if (strops('/google.com/', $referral)) {
require_once ('b.php');
} else {
require_once ('c.php');
}
?>

但它不起作用:(

<?php
$referral = $_SERVER['HTTP_REFERER'];
if ((strpos($referral, 'yahoo.com/main.html')!==false)
  ||(strpos($referral, 'yahoo.com/secound.html')!==false)) {
require_once ('a.php');
} else if (strpos($referral, 'google.com')!==false) {
require_once ('b.php');
} else {
require_once ('c.php');
}
?>

查看这里的 PHP 文档:http://php.net/manual/en/function.strpos.php

Strpos 在另一个字符串中找到一个特定的字符串,因此你不能使用正则表达式。你可以找到一个特定的字符串。

例如

strpos('google.com', $referral')

将返回包含 google.com 的任何字符串的true。如果你想检测多个不同的字符串,你可以将多个strpos组合在一起(使用或运算符(,或者坚持使用当前的方法。