How can PHP determine if a string contains a specific substring?

In PHP, you can use the strpos() function to check if a string contains another string. An example is shown below:

$string = 'Hello, World!';
$subString = 'Hello';

if (strpos($string, $subString) !== false) {
    echo "字符串包含该子字符串";
} else {
    echo "字符串不包含该子字符串";
}

In the example above, we are using the strpos() function to locate the position of a substring within a string. If the substring is found within the string, the function will return the position of the substring in the string, otherwise it will return false. Use the return value to determine if the string contains a specific substring.

Leave a Reply 0

Your email address will not be published. Required fields are marked *