doucan4815 2013-03-26 05:06
浏览 51
已采纳

用preg_match检查一些单词

I need to check some words with preg_match. If the words contain numbers and ., I want to echo "ok".

Lets say i want to ckeck this :

4814.84dszs  //ok
412.84.61.412  //ok
hello.4you //ok
welcome.user //not ok
481221222 // not ok

I have used this:

if (preg_match('/^[0-9][.]+$/', 'MY_WORD_HERE'))
{
echo 'ok';
}

But it doesn't give me the exactly result which I looking for.

  • 写回答

4条回答 默认 最新

  • douyingp82418 2013-03-26 05:22
    关注

    Try this :

    if (preg_match('/(\d+\.)|(\.\d+)/', '4814.84dszs'))
    {
    echo 'ok';
    }
    

    ^ -> indicates at the begining of the string

    $ -> End of the string.

    So your pattern says from start to end(Matching for whole string)

    If you remove this it will match anywhere in the string not whole string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件