Home > Backend Development > PHP Tutorial > 排除字符串里某字符第3次出现后面的所有字符,代码怎么写

排除字符串里某字符第3次出现后面的所有字符,代码怎么写

WBOY
Release: 2016-06-23 14:20:31
Original
772 people have browsed it

排除字符串里某字符第3次出现后面的所有字符,代码怎么写

比如字符串“2你好2哈哈2对的啊哈哈哈”

就是排除第3个2后面的所有字符得到结果“2你好2哈哈2"

请知道的大哥直接给代码好吗谢谢了 


回复讨论(解决方案)

(2)([^2]*)\1\2\1

$s='2你好2哈哈2对的啊哈哈哈';$ar=explode('2',$s);echo implode('2',array_merge(array_slice($ar,0,3),array('')));
Copy after login
Copy after login
Copy after login

$s = '2你好2哈哈2对的啊哈哈哈';echo preg_replace('/((?:.*?2){3}).*/', '$1', $s);
Copy after login
2你好2哈哈2

<?phpheader("content-type:text/html;charset=utf-8");$sep = '2' ;$str = "2你好2哈哈2对的啊哈哈哈";preg_match("/(.*?$sep){3}/",$str,$match);echo $match[0];
Copy after login


$s='2你好2哈哈2对的啊哈哈哈';$ar=explode('2',$s);echo implode('2',array_merge(array_slice($ar,0,3),array('')));
Copy after login
Copy after login
Copy after login


非常感谢这位大哥,再排除最后一个2自身代码怎么写


$s='2你好2哈哈2对的啊哈哈哈';$ar=explode('2',$s);echo implode('2',array_merge(array_slice($ar,0,3),array('')));
Copy after login
Copy after login
Copy after login


非常感谢这位大哥,再排除最后一个2自身代码怎么写

$s='2你好2哈哈2对的啊哈哈哈';$ar=explode('2',$s);echo implode('2',array_slice($ar,0,3));
Copy after login

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