Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 确保字符串没有';不包含http,也不包含';不要以正斜杠开始_Php_Regex_Preg Match - Fatal编程技术网

Php 确保字符串没有';不包含http,也不包含';不要以正斜杠开始

Php 确保字符串没有';不包含http,也不包含';不要以正斜杠开始,php,regex,preg-match,Php,Regex,Preg Match,我试图将根url附加到重定向url,但前提是它不包含http或https,并且/code>不以/code>开头 我有一个似乎有效的代码: 但我想知道,我不应该使用和,而不是字符来表示或吗?但我不确定在正则表达式中如何使用?您可以避免使用使用此正则表达式: ^(https?:/)?/ 代码: if (!preg_match('#^(https?:/)?/#', $redirect_url)) { $redirect_url = 'http://' . $redirect_url; }

我试图将根url附加到重定向url,但前提是它不包含
http
https
,并且
/code>不以
/code>开头

我有一个似乎有效的代码:


但我想知道,我不应该使用
,而不是
字符来表示
吗?但我不确定在正则表达式中如何使用?

您可以避免使用
使用此正则表达式:

^(https?:/)?/
代码:

if (!preg_match('#^(https?:/)?/#', $redirect_url)) {
    $redirect_url = 'http://' . $redirect_url;
}

使用此正则表达式可以避免使用
|

^(https?:/)?/
代码:

if (!preg_match('#^(https?:/)?/#', $redirect_url)) {
    $redirect_url = 'http://' . $redirect_url;
}

把你的正则表达式改成

if (!preg_match('#^(?:https?://|/)#', $redirect_url)) {
    $redirect_url = 'http://' . $redirect_url;
}

只需将正则表达式更改为

if (!preg_match('#^(?:https?://|/)#', $redirect_url)) {
    $redirect_url = 'http://' . $redirect_url;
}

我不确定我是否理解您的需要,但这就是您想要的吗

if (preg_match('#(?!.*https?://)(?!^/)#', $redirect_url)) {
    $redirect_url = 'http://' . $redirect_url;
}

我不确定我是否理解你的需要,但这就是你想要的吗

if (preg_match('#(?!.*https?://)(?!^/)#', $redirect_url)) {
    $redirect_url = 'http://' . $redirect_url;
}

如果您不想同时使用这两个选项,则
将执行此操作。!(x或y)是您想要的。无论如何,它不能同时以http和/开头。如果您不想同时使用http和/或http,则
将执行此操作。!(x或y)是您想要的。无论如何,它不能同时以http和/启动。nvm。。我想我明白了:)是的,当然。它匹配行开始使用
^
,然后使用
http://
https://
,因为我们有可选的
s
使用
s?
。还要记住
之后的
(https?:/)?
使整个事情成为可选的。最后,它匹配使其匹配的下一个
/
http://
https://
或行开始处的
/
。有关更多说明,您也可以查看我的演示链接中的说明。谢谢您的解释。似乎我理解正确:)nvm。。我想我明白了:)是的,当然。它匹配行开始使用
^
,然后使用
http://
https://
,因为我们有可选的
s
使用
s?
。还要记住
之后的
(https?:/)?
使整个事情成为可选的。最后,它匹配使其匹配的下一个
/
http://
https://
或行开始处的
/
。有关更多说明,您也可以查看我的演示链接中的说明。谢谢您的解释。似乎我理解正确:)