我需要找到在连字符前截断标题的方法,如下所示:
"This is the title-This is an example"因此,当它被截断时,标题将如下所示:
"This is the title"(没有"-This is an example"部件。)
我想有一个PHP函数可以做到这一点,但我没有找到它。
发布于 2011-12-27 09:23:41
您可以使用几种不同的方法。我个人的偏好可能是strtok(),因为它需要的代码量最少,而且效率很高
strtok()
$title = strtok($str, '-');CodePad
explode()。
$title = current(explode('-', $str, 2));CodePad。
substr()和strpos()
$title = substr($str, 0, strpos($str, '-'));CodePad。
strstr()
如果使用的是>= PHP5.3。
$title = strstr($str, '-', TRUE);CodePad。
发布于 2011-12-27 09:20:04
您正在寻找explode函数。
顺便说一句,a-是连字符,不是斜杠。
https://stackoverflow.com/questions/8640350
复制相似问题