PHP:如何判断字符串是否包含特殊字符?

发布于 2024-09-10 11:39:23 字数 229 浏览 4 评论 0原文

我正在使用 pspell 来检查一些单词的拼写。但是,如果该单词类似于 G3523B,那么它显然不是拼写错误的单词,但 pspell 将其更改为 GB。我想在尝试拼写检查之前以某种方式将一个单词限定为单词。也许检查字符串是否包含任何数字或特殊字符。

那么检查字符串中的特殊字符或数字的最佳方法是什么?

(如果有人有更好的想法来实现我的目标,请分享)

I am using pspell to spell check some words. However if the word is something like G3523B it clearly is not a misspelled word but pspell changes it to GB. I would like somehow to qualify a word as a word before trying to spell check it. Maybe checking to see if the string contains any numbers or special characters.

So what is the best way to check a string for special chars or digits?

(if someone has a better idea to achieve what I am after please share)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-09-17 11:39:23

使用正则表达式怎么样:

if (preg_match('/[^a-zA-Z]+/', $your_string, $matches))
{
  echo 'Oops some number or symbol encountered !!';
}
else
{
  // Everything fine... carry on
}

How about using a regex:

if (preg_match('/[^a-zA-Z]+/', $your_string, $matches))
{
  echo 'Oops some number or symbol encountered !!';
}
else
{
  // Everything fine... carry on
}
荒岛晴空 2024-09-17 11:39:23

如果您只想检查字符串 $input 是否仅包含字符 az 和 AZ,您可以使用以下命令:

if(!preg_match('/^\[a-zA-Z]+$/',$input)) {
   // String contains not allowed characters ...
}

If you just want to check whether the string $input consists only of characters a-z and A-Z you can use the following:

if(!preg_match('/^\[a-zA-Z]+$/',$input)) {
   // String contains not allowed characters ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文