php - 求个获取指定字符之后的所有字符串 但是不包含指定字符的方法
ringa_lee
ringa_lee 2017-04-10 17:41:41
0
3
882

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

ringa_lee
ringa_lee

ringa_lee

reply all(3)
PHPzhong

可以这样做(参考)

<?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);
大家讲道理

使用正则匹配:

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

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

Array
(
    [0] => Chongqing 123456!
    [right] =>  123456!
    [1] =>  123456!
)
大家讲道理
$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));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template