简体   繁体   English

正则表达式允许撇号

[英]Regex expression to allow an apostrophe

I've been looking at all of the other posts regarding this topic but I'm still unable to get my regex expression to allow an apostrophe. 我一直在查看有关此主题的所有其他帖子,但我仍然无法获得我的正则表达式以允许撇号。 I thought the following should work but it keeps giving an error. 我认为以下应该可以工作,但它一直出错。 It does allow all of the other characters, numbers and letters, just not the ' (I need it for words like Mike's ) 它允许所有其他字符,数字和字母,而不是'(我需要它像迈克的话)

/^([-a-zA-Z\/\-\&\\\?\!\,\.\'\"\s0-9@:=_]{1,1000})$/

The regex expression is required for validation of a form and part of the following: 验证表单和以下部分内容需要正则表达式:

public $regex = array ('char' => "/^[a-zA-Z.\s]{0,50}$/", 
                       'misc' => "/^([-a-zA-Z\/\-\&\\\?\!\,\.\'\"\s0-9@:=_]{1,1000})$/");

if(preg_match($this->regex[$validation],$this->filteredValue[$fieldName])) { 
    $this->messageArray[$fieldName] = '<span class="cheers"> &nbsp; Thank You</span>';   
}

Can anybody tell me where I am going wrong please? 谁能告诉我哪里出错了?

The \\' should work. \\'应该工作。 I can't say why it does not. 我不能说为什么不这样做。 I can, however, offer a workaround. 但是,我可以提供一种解决方法。 You can try to replace the ' with a hex representation of the same character. 您可以尝试将'替换为具有相同字符的十六进制表示。 instead of using \\' try to use \\x1b . 而不是使用\\'尝试使用\\x1b

Also, you should move the \\- to be just a - at the end of the [] 另外,你应该将[ \\-移动到\\-只是-[]的末尾

/^([-a-zA-Z\/\&\\\?\!\,\.\x1b\"\s0-9@:=_-]{1,1000})$/

And I don't understand what's the - at the beginning is... 我不明白什么是-一开始是......


EDIT: After seeing your last comment regarding the error that you get I can only assume that you have mis-identified the problem that you have. 编辑:看到你对你得到的错误的最后评论后,我只能假设你错误地发现了你所遇到的问题。 For what I see assume that your regexp is all fine, but you have a problem in your SQL statment as you have not escaped the data. 对于我所看到的假设您的正则表达式都很好,但是您的SQL语句中存在问题,因为您没有转义数据。

You should familiraize yourself with mysql_real_escape_string and make sure you use it in all your queries. 您应该使用mysql_real_escape_string来熟悉自己,并确保在所有查询中使用它。 For Example: 例如:

<?php
// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),
            mysql_real_escape_string($password));
mysql_query($query);

?>

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

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