Home  >  Article  >  Backend Development  >  求一段正则,该怎么处理

求一段正则,该怎么处理

WBOY
WBOYOriginal
2016-06-13 13:01:18745browse

求一段正则
需求:写模版引擎
原来的字符串:sfsdafsadfsadfsdfsdfsdfs{任意字符}gdfgdsfgdfgdf
其中任意字符里必须是a到z和-开头
如 {time()}替换成 ,,,,, {file_get_contents}替换成
注意:{}里面的()可有可无,最多一次


我是这样写的,一直错误
$new_str=preg_replace('/\{\s*([a-z|A-Z]*)\s*\}/is','',$new_str);
输出:


请按照我这样写 必须有\\1这样的
------解决方案--------------------
$new_str = 'dsafs{aaa}fff{time()}xxx';
echo $new_str=preg_replace('/\{\s*([a-z]+)(?:(\(\)))?\s*\}/isU','',$new_str);

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