Home >Backend Development >PHP Tutorial >如何动态截掉字符串”1|10,2|11,3|12“中的|和,号之间的部分,包括|?

如何动态截掉字符串”1|10,2|11,3|12“中的|和,号之间的部分,包括|?

WBOY
WBOYOriginal
2016-06-23 13:51:52974browse

如何用PHP,动态截掉字符串”1|10,2|11,3|12“中的|和,号之间的部分,包括|?


回复讨论(解决方案)

str_replace(",","",str_replace("|","",$val)) 替换即可

$s = "1|10,2|11,3|12";echo preg_replace('/\|[^,]+,/', ',', $s);
1,2,3|12

结果是110211,我要的结果是1|10,2|11,3|12 变成1,2,3 拜托!

如何再去掉|12变成1,2,3呢?

$content = '1|10,2|11,3|12';echo preg_replace(array('/\|[^,]+,/','/\|[^,]+$/'), array(',',''), $content);

1,2,3

$s = "1|10,2|11,3|12";echo preg_replace('/\|[^,]+/', '', $s);
1,2,3

但这并不符合: 截掉 | 和 , 号之间的部分

谢谢,结贴!

Statement:
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