doucheng5209 2014-02-21 15:37
浏览 15
已采纳

如何查找变量是否包含某些字符? PHP [重复]

This question already has an answer here:

Intro

In search, i want to make search for age range.

if a user inserts a '-' included input like 10-30 this way i will display list of people age greater than 10 and less than 30.

My Question

$q = mysql_real_escape_string(htmlspecialchars("user input from search field"));

From here before entering to make query i want to make two conditions, One: if there's a '-' in user input or not ? if yes i want to query as according, if not the other way.

So, how can i enter into the first condition ?

</div>
  • 写回答

2条回答 默认 最新

  • doubu2730 2014-02-21 15:40
    关注

    This will work:

    // use strpos() because strstr() uses more resources
    if(strpos("user input from search field", "-") === false)
    {
        // not found provides a boolean false so you NEED the ===
    }
    else
    {
        // found can mean "found at position 0", "found at position 19", etc...
    }
    

    strpos()

    What NOT to do

    if(!strpos("user input from search field", "-"))
    

    The example above will screw you over because strpos() can return a 0 (zero) which is a valid string position just as it is a valid array position.

    This is why it is absolutely mandatory to check for === false

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

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)