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

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

WBOY
Release: 2016-06-06 20:08:21
Original
1329 people have browsed it

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

回复内容:

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

可以这样做(参考)

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

或者

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

使用正则匹配:

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

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

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

<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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template