lau*_*kok 5 php regex preg-match
我希望有一个正则表达式,确保字符串的开头包含'http://'和'/'以及结尾.
这是我提出的更长的版本,
if(!preg_match("/(^http:\/\//", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start."/>';
}
elseif (!preg_match("/\/$/", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link has ended with a /."/>';
}
Run Code Online (Sandbox Code Playgroud)
但我认为这两个表达式可以像下面这样放在一起,但它不会起作用,
if(!preg_match("/(^http:\/\/)&(\/$)/", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start and a / at the end."/>';
}
Run Code Online (Sandbox Code Playgroud)
我试图结合的多个表达式一定是错的!任何的想法?
谢谢,刘
Gre*_*con 10
if(preg_match('/^http:\/\/.*\/$/', $site_http))
{
...
}
Run Code Online (Sandbox Code Playgroud)
该^http:\/\/部队http://在正面的\/$力量在最后一个斜杠,并.*让一切(也可能没有)之间.
例如:
<?php
foreach (array("http://site.com.invalid/", "http://") as $site_http) {
echo "$site_http - ";
if (preg_match('/^http:\/\/.*\/$/', $site_http)) {
echo "match\n";
}
else {
echo "no match\n";
}
}
?>
Run Code Online (Sandbox Code Playgroud)
生成以下输出:
http://site.com.invalid/ - match http:// - no match
| 归档时间: |
|
| 查看次数: |
8232 次 |
| 最近记录: |