dtzk85937 2012-06-27 11:44
浏览 114
已采纳

如果string包含正斜杠

How do i make a if statement which checks if the string contains a forward slash?

$string = "Test/Test";   
if($string .......)
{
    mysql_query(""); 
} 
else 
{
    echo "the value contains a invalid character"; 
} 
  • 写回答

3条回答 默认 最新

  • doumi6685 2012-06-27 11:46
    关注

    You can use strpos, which will make sure there is a forward slash in the string but you need to run it through an equation to make sure it's not false. Here you can use strstr(). Its short and simple code, and gets the job done!

    if(strstr($string, '/')){
        //....
    }
    

    For those who live and die by the manual, when the haystack is very large, or the needle is very small, it is quicker to use strstr(), despite what the manual says.

    Example:

    Using strpos(): 0.00043487548828125

    Using strstr(): 0.00023317337036133

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

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示