本文实例讲述了PHP针对字符串开头和结尾的判断方法。分享给大家供大家参考,具体如下:
//变量: $s1 = "dotcoo world!"; $s2 = "dotcoo"; $s3 = "hello dotcoo"; //php判断字符串开头: var_dump(substr($s1, 0, strlen($s2)) === $s2); var_dump(strpos($s1, $s2) === 0); var_dump(strncmp($s1, $s2, strlen($s2)) === 0); var_dump(substr_compare($s1, $s2, 0, strlen($s2)) === 0); //php判断字符串结尾: var_dump(substr($s1, 0, strlen($s2)) === $s2); var_dump(substr_compare($s3, $s2, -strlen($s2)) === 0);
希望本文所述对大家PHP程序设计有所帮助。