简体   繁体   English

php RegEx在我的本地服务器上工作-在运行php4的远程主机上不工作

[英]php RegEx working on my local server - not working on remote host running php4

I am pretty sure I know what the issue is, but I cannot resolve it for the life of me and I just don't know what to do at this point. 我敢肯定,我知道问题出在哪里,但我无法终生解决,我只是不知道该怎么办。

All I am trying to do is qualify the input of a first and last name field. 我要做的就是限定名字和姓氏字段的输入。 This code IS working on my local server running php5: 此代码在运行php5的本地服务器上有效:

    <?php 
$regex = "John's Jerod";
 if (!preg_match("/^[A-Za-z\'\,\.\s]+$/", $regex)) {
          echo "ERROR!";
      } else {
   echo "NO ERROR!";
   }
?>

As expected, this returns NO ERROR!, but when I run this on my live server, I only get ERROR with the same data. 如预期的那样,这将返回NO ERROR !,但是当我在实时服务器上运行该命令时,我只会得到具有相同数据的ERROR。

I've determined it's the comma throwing it off! 我确定是用逗号把它扔掉了! And I am pretty sure, because the php version I am running does an auto escape like \\' in the name John\\'s, so I ran striptags on all output and still the same error. 而且我很确定,因为我正在运行的php版本会以John \\'s的名称进行自动转义,例如\\',所以我在所有输出上都运行了剥离标签,并且仍然出现相同的错误。

DOes anyone know what I am doing wrong or how I can resolve this? 有谁知道我在做什么错或如何解决这个问题?

I've got about 8 hours in the "bug" as of now. 到目前为止,我在“错误”中的时间大约为8个小时。 Been through 40+ variations of the RegEex with no luck. 经历了40多个RegEex变体,没有运气。

I check and triple checked all of my form field names, vars etc to ensure everything matcheds and all else is OK. 我检查并三重检查了我所有的表单字段名称,vars等,以确保所有内容都匹配并且其他所有都正常。

SOS 紧急求救

Try checking specifically for 1 . 尝试专门检查1 Per the documentation, it returns FALSE (strict check) on error, 0 (strict check) for no matches, or 1 if there is a match (since it stops at one). 根据文档,如果有错误,它将返回FALSE (严格检查);如果没有匹配项,则返回0 (严格检查);如果存在匹配项,则返回1 (因为它停止为1)。

Also, per my own preference, I use the ~ symbol for my regex's. 另外,根据自己的喜好,我在正则表达式中使用〜符号。 And like David Powers said (only he didn't correct it at all), you don't need most of those backslashes (only for the period and the space). 就像大卫·鲍尔斯(David Powers)所说的(只是他根本没有纠正它),您并不需要大多数的反斜杠(仅针对句号和空格)。

<?php 
$regex = "John's Jerod";
 if (preg_match("~^[A-Za-z',\.\s]+$~", $regex) !== 1) {
          echo "ERROR!";
      } else {
   echo "NO ERROR!";
   }
?>

Hope this helps! 希望这可以帮助!

EDIT: Also, you say you're using strip_tags ? 编辑:另外,您说您正在使用strip_tags That strips any HTML tags in a string -- not slashes. 这会剥离字符串中的所有HTML标记-而不是斜线。 You need strip_slashes to strip slashes, not strip_tags ;) 您需要strip_slashes斜杠, 而不是 strip_tags ;)

Your regex doesn't need all those backslashes. 您的正则表达式不需要所有这些反斜杠。 It should be this: 应该是这样的:

"/^[A-Za-z',\.\s]+$/"

Also, you refer to using striptags(). 另外,您指的是使用striptags()。 You should be using stripslashes() if your server has magic quotes enabled. 如果您的服务器启用了魔术引号,则应该使用stripslashes()。

[Edited regex] [正则表达式编辑]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM