PHP-如何检查字符串是否包含特定文本 [英] PHP - How to check if a string contains a specific text

查看:93
本文介绍了PHP-如何检查字符串是否包含特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
$a = '';

if($a exist 'some text')
    echo 'text';
?>

假设我有上面的代码,如何编写语句"if($ a存在某些文本")"?

Suppose I have the code above, how to write the statement "if($a exist 'some text')"?

推荐答案

使用strpos函数:

在您的情况下:

if( strpos( $a, 'some text' ) !== false ) echo 'text';

请注意,我之所以使用!==运算符(而不是!= false== true甚至只是if( strpos( ... ) ) {)是因为本质是PHP处理strpos的返回值的原因.

Note that my use of the !== operator (instead of != false or == true or even just if( strpos( ... ) ) {) is because of the "truthy"/"falsy" nature of PHP's handling of the return value of strpos.

这篇关于PHP-如何检查字符串是否包含特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆