PHP中的标签验证 - 允许输入字段为空?
The Script:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="text" name="hashtag1" />
<input type="text" name="hashtag2" />
<input type="text" name="hashtag3" />
<input type="text" name="hashtag4" />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST['submit'])){
if(isset($_POST['hashtag1'])) {
$hashtag1 = $_POST['hashtag1'];
}
if(isset($_POST['hashtag2'])){
$hashtag2 = $_POST['hashtag2'];
}
if(isset($_POST['hashtag3'])){
$hashtag3 = $_POST['hashtag3'];
}
if(isset($_POST['hashtag4'])){
$hashtag4 = $_POST['hashtag4'];
}
$myarray = array($hashtag1, $hashtag2, $hashtag3, $hashtag4);
for($i = 0; $i < count($myarray); $i++){
if(!preg_match('/^(?=.{2,140}$)(#|\x{ff03}){1}([0-9_\p{L}]*[_\p{L}][0-9_\p{L}]*)$/u', $myarray[$i])){
echo "Please check your hashtags!";
exit;
}
}
function array_has_dupes($array) {
return count($array) !== count(array_unique($array));
}
if(array_has_dupes($myarray) == true){
echo "Please make sure you do not have duplicate hashtags!";
exit;
}
echo "Hello World!";
}
?>
When I leave one input field of those four input fields empty, then I get "Please check your hashtags." printed on screen.
My question is, how to modify the script so leaving one or more input fields empty is allowed?
I am using the "PHP Twitter Hashtag Validation Regex" from GitHub: link
脚本: strong> p>
当我将的那些 em>四个输入字段的输入字段留空时, 然后 em> strong> 我在屏幕上打印“请检查您的主题标签。” em>。 p>
我的问题是,如何修改脚本以便保留一个或多个 em>允许输入字段为空? p>
我正在使用GitHub的“PHP Twitter Hashtag Validation Regex”:链接 p>
div>
&lt; form method =“post”action =“&lt;?php echo $ _SERVER ['PHP_SELF'];?&gt;” &gt;
&lt; input type =“text”name =“hashtag1”/&gt;
&lt; input type =“text”name =“hashtag2”/&gt;
&lt; input type =“text”name = “hashtag3”/&gt;
&lt; input type =“text”name =“hashtag4”/&gt;
&lt; input type =“submit”name =“submit”/&gt;
&lt; / form&gt;
&lt;?php
if(isset($ _ POST ['submit'])){
if(isset($ _ POST ['hashtag1'])){
$ hashtag1 = $ _POST [ 'hashtag1'];
}
if(isset($ _ POST ['hashtag2'])){
$ hashtag2 = $ _POST ['hashtag2'];
}
if(isset($ _ POST [' hashtag3'])){
$ hashtag3 = $ _POST ['hashtag3'];
}
if(isset($ _ POST ['hashtag4'])){
$ hashtag4 = $ _POST ['hashtag4'] ;
}
$ myarray = array($ hashtag1,$ hashtag2,$ hashtag3,$ hashtag4);
for($ i = 0; $ i&lt; count($ myarray); $ i ++) {
if(!preg_match('/ ^(?=。{2,140} $)(#| \ x {ff03}){1}([0-9_ \ p {L}] * [_ \ p {L}] [0-9_ \ p {L}] *)$ / u',$ myarray [$ i])){
echo“请检查你的主题标签!”;
退出;
}
}
函数array_has_dup es($ array){
return count($ array)!== count(array_unique($ array));
}
if(array_has_dupes($ myarray)== true){
echo“Please 确保你没有重复的标签!“;
exit;
}
echo”Hello World!“;
}
?&gt;
code> pre >
Use a variable to keep track of a valid hashtag, define it as false before your loop. If you find a valid hashtag, break out of your loop and set your variable to true.
After your loop, check the variable and if it's still false, display your error and exit:
$atLeastOne = false;
for($i = 0; $i < count($myarray); $i++){
if(preg_match('/^(?=.{2,140}$)(#|\x{ff03}){1}([0-9_\p{L}]*[_\p{L}][0-9_\p{L}]*)$/u', $myarray[$i])){
$atLeastOne = true;
break;
}
}
if(!$atLeastOne){
echo "Please check your hashtags!";
exit;
}
Note we are checking for a valid hashtag here, so you need to remove ! from within your if statement.