Home > Backend Development > PHP Tutorial > 正则表达式求救

正则表达式求救

WBOY
Release: 2016-06-23 13:33:50
Original
952 people have browsed it

[#微笑][#大哭][#色] 如果通过preg_replace() 变成正则表达式求救正则表达式求救正则表达式求救


回复讨论(解决方案)

$str='[#微笑][#大哭][#色]';preg_match_all('/\[#(.+?)\]/',$str,$m);echo '<img  src="'.$m[1][0].'.jpg"/ alt="正则表达式求救" ><img  src="'.$m[1][1].'.jpg"/ alt="正则表达式求救" ><img  src="'.$m[1][2].'.jpg"/ alt="正则表达式求救" >';
Copy after login

我的$str里面的表情数量是不定的,而且还会有其他非表情的字符

如:$str=我来啦!![#微笑][#微笑]么么~~[#色] 变成:我来啦!!正则表达式求救正则表达式求救么么~~正则表达式求救

网上看了个例子:$str = preg_replace ( "[\[#([0-9]*)\]]", "正则表达式求救", $str );
但是这个只能替换[#1][#22]这种 如果是中文的 要怎么改呢?正则一点都不懂啊

$str='af d [#微笑]adfaf[#大哭]asdfasd[#色]asdfaf';$new = preg_replace('/\[\#(.+?)\]/', "<img  src='$1.jpg'/ alt="正则表达式求救" >", $str);var_dump($new);
Copy after login
Copy after login

$str='af d [#微笑]adfaf[#大哭]asdfasd[#色]asdfaf';$new = preg_replace('/\[\#(.+?)\]/', "<img  src='$1.jpg'/ alt="正则表达式求救" >", $str);var_dump($new);
Copy after login
Copy after login



这个可以!感谢!!

$str = "[#微笑][#大哭][#色]";
$pattern = "/\[#([\s\S]*)\]\s*\[#([\s\S]*)\]\s*\[#([\s\S]*)\]\s*/";
preg_match_all($pattern,$str,$match);
$new_str = preg_replace($pattern,'正则表达式求救正则表达式求救正则表达式求救',$str);
echo $new_str;

Related labels:
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