Home  >  Article  >  Backend Development  >  求个获取指定字符之后的所有字符串 但是不包含指定字符的方法

求个获取指定字符之后的所有字符串 但是不包含指定字符的方法

WBOY
WBOYOriginal
2016-06-06 20:08:211285browse

strstr获取指定字符之后的所有字符串,其中是包含那个指定字符的,求个不包含指定字符的方法

回复内容:

strstr获取指定字符之后的所有字符串,其中是包含那个指定字符的,求个不包含指定字符的方法

可以这样做(参考)

<code><?php $allString = "I love Shanghai 123456!";
$searchString = "Shanghai";
$newString = strstr($allString, $searchString);
$length = strlen($searchString);
echo substr($newString, $length);
</code></code>

或者

<code><?php $allString = "I love Shanghai 123456!";
$searchString = "Shanghai";
$firstLength = strpos($allString, $searchString);
$length = $firstLength + strlen($searchString);
echo substr($allString, $length);
</code></code>

使用正则匹配:

<code><?php $allString = "I love Chongqing 123456!";
preg_match("/Chongqing(?<right>.*)/", $allString, $matches);
print_r($matches);</code>

其中,right只是给匹配的结果命名,最终输出:

<code>Array
(
    [0] => Chongqing 123456!
    [right] =>  123456!
    [1] =>  123456!
)</code>

<code>$str = 'I love guangdong 123456!';
$strDelimiter = 'guangdong';
var_dump(preg_split('/'.preg_quote($strDelimiter,'/').'/',$str));

# 如果确定strDelimiter只出现一次
var_dump(strrev(strstr(strrev($str),strrev($strDelimiter),true)));

# 替换
var_dump(preg_replace('/.+?'.preg_quote($strDelimiter,'/').'/','',$str));
</code>
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