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

WBOY
Libérer: 2016-06-06 20:08:21
original
1296 Les gens l'ont consulté

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

回复内容:

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

可以这样做(参考)

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

或者

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

使用正则匹配:

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

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

<code>Array
(
    [0] => Chongqing 123456!
    [right] =>  123456!
    [1] =>  123456!
)</code>
Copier après la connexion

<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>
Copier après la connexion
Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!