简体   繁体   English

使用复合词的PHP中标签的正则表达式

[英]Regular expression for tags in PHP using compound words

Here is my code so far: 到目前为止,这是我的代码:

if (preg_match('/\s/',$tagName)){
    $_ErrorMessage = "<div class='alert alert-danger' id='errorBox'>";
    $_ErrorMessage.= "<b>Error!</b> La etiqueta no puede contener espacios. Asegúrese de escribirla correctamente.";
    $_ErrorMessage.= "</div>";
} elseif(preg_match('/[^A-Z]/i', $tagName)){
    $_ErrorMessage = "<div class='alert alert-danger' id='errorBox'>";
    $_ErrorMessage.= "<b>Error!</b> La etiqueta no puede contener carácteres especiales. Cáracteres especiales son todos aquellos que no estan dentro del alfabeto. Por favor escríbala de nuevo.";
    $_ErrorMessage.= "</div>";
} else {
    $insertNewTagQuery = sprintf("INSERT IGNORE INTO solution_tags (SOLUTION_TAGS_NAME)VALUES(LOWER('%s'))", $tagName);
    if($DBConnect->query($insertNewTagQuery)){
        header("Location:http://127.0.0.1/helpdesk/admin/thankyou.php");
    }
}

With the code above, I'm checking whether the string where the tag is, has blank spaces and whether it has any special characters other than the ones in the alphabet. 通过上面的代码,我正在检查标记所在的字符串是否具有空格,并且是否具有除字母之外的任何特殊字符。

Giving it a second thought, and after the engineer in the class said that tags can be compound words and have special characters and numbers such as Internet Explorer or 'google-chrome' or HTML5 I decided to modify this tiny script. 再三考虑,在班上的工程师说标签可以是复合词并具有特殊字符和数字(例如Internet Explorer或“ google-chrome”或HTML5我决定修改此小脚本。

The problem is that I have no experience whatsoever on building regular expressions. 问题是我对构建正则表达式没有任何经验。

How can I do a regular expression with the following criteria? 如何使用以下条件进行正则表达式?

  • Compound words 复合词
  • Dash special character in the middle of two words (internet-explorer, google-chrome) 在两个单词中间插入破折号特殊字符(互联网浏览器,谷歌浏览器)
  • No white spaces at the beginning or end of the string 字符串开头或结尾没有空格

这是一个满足您要求的PHP正则表达式:

/^\b[-a-z \d]*\b$/i

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

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