PHP字符串开头和结尾

2016-01-21 22:32  968人阅读  评论 (0)
Tags: 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);