PHP-如何检查字符串是否包含特定文本 [英] PHP - How to check if a string contains a specific text
本文介绍了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')"?
推荐答案
使用 在您的情况下: 请注意,我之所以使用 Note that my use of the 这篇关于PHP-如何检查字符串是否包含特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!strpos
函数:
if( strpos( $a, 'some text' ) !== false ) echo 'text';
!==
运算符(而不是!= false
或== true
甚至只是if( strpos( ... ) ) {
)是因为本质是PHP处理strpos
的返回值的原因.!==
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
.
查看全文