Home  >  Article  >  Backend Development  >  字符串替换解决思路

字符串替换解决思路

WBOY
WBOYOriginal
2016-06-13 13:48:37861browse

字符串替换
有一段字符串,存在多个"#xxxxx#"这样的子串,我想把这样的子串转换成其他的字符串,应该怎么做?

#xxxxx#转成 字符串替换解决思路 ,就是把##里面的字符取出来 然后把整个#xxxxx#替换成 字符串替换解决思路

------解决方案--------------------
不用正则的话

$html='#xxxxx##xxxxx#';
$str="#xxxxx#";
$str=str_replace('#xxxxx#',' 字符串替换解决思路 ',$html);
echo $str;

------解决方案--------------------

探讨
不用正则的话

$html='#xxxxx##xxxxx#';
$str="#xxxxx#";
$str=str_replace('#xxxxx#',' 字符串替换解决思路 ',$html);
echo $str;

------解决方案--------------------
探讨

引用:
不用正则的话

$html='#xxxxx##xxxxx#';
$str="#xxxxx#";
$str=str_replace('#xxxxx#',' 字符串替换解决思路 ',$html);
echo $str;
用正则呢?求指教

------解决方案--------------------
echo preg_replace('/#([^#]+?)#/is', ' 字符串替换解决思路 ', $html);
------解决方案--------------------
replace(被替换的字符串,代替的字符串),正则就要看你被替换和替换的都是什么了,推荐用replace
------解决方案--------------------
修饰符/u 什么意思
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