Rumah > Soal Jawab > teks badan
strstr获取指定字符之后的所有字符串,其中是包含那个指定字符的,求个不包含指定字符的方法
PHPz2017-04-10 17:43:41
可以这样做(参考)
<?php
$allString = "I love Shanghai 123456!";
$searchString = "Shanghai";
$newString = strstr($allString, $searchString);
$length = strlen($searchString);
echo substr($newString, $length);
或者
<?php
$allString = "I love Shanghai 123456!";
$searchString = "Shanghai";
$firstLength = strpos($allString, $searchString);
$length = $firstLength + strlen($searchString);
echo substr($allString, $length);
大家讲道理2017-04-10 17:43:41
使用正则匹配:
<?php
$allString = "I love Chongqing 123456!";
preg_match("/Chongqing(?<right>.*)/", $allString, $matches);
print_r($matches);
其中,right只是给匹配的结果命名,最终输出:
Array
(
[0] => Chongqing 123456!
[right] => 123456!
[1] => 123456!
)
大家讲道理2017-04-10 17:43:41
$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));