Home  >  Article  >  Backend Development  >  如何把未知长度的字符串从末尾截掉指定的字串呢?

如何把未知长度的字符串从末尾截掉指定的字串呢?

WBOY
WBOYOriginal
2016-06-23 14:03:08791browse

如:字串:沈阳市公安局电话 想截掉尾部的“电话”,如何实现呢?


回复讨论(解决方案)

preg_replace('/电话$/')
trim()
substr+strpos

楼上,能详细说下吗?比如s=沈阳市公安局电话 你提到的4个函数都得用上?

trim() http://www.w3school.com.cn/php/func_string_trim.asp
strpos() http://www.w3school.com.cn/php/func_string_stripos.asp
substr http://www.w3school.com.cn/php/func_string_substr.asp
perg_replace http://php.net/manual/en/function.preg-replace.php
请善用google

$s='沈阳市公安局电话';
echo preg_replace('/电话$/','',$s);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP怎么同时查询多个ID?Next article:php 字符串操作