2011-04-02 75 views
1

我该如何逃避?逃离preg_replace的+号

/{\([^.{}]*)-}/ 

而这个?

/{\([^.{}]*)+}/ 
+0

你想摆脱什么,或为什么你必须逃避那些? – salathe 2011-04-02 13:56:09

+0

salathe,我发现当URL编码序列化的会话数据时,+符号必须被转义。所以OP的问题是有效的。 – deweydb 2012-08-21 22:12:25

回答

1

你需要躲避括号,在括号加号 - 如果你想从字面上匹配。然后你的正则表达式的PHP代码将是:

$output = preg_replace('/\{\([^.{}]*\)-\}/', 'replace-string', $input); 

......还有......

$output = preg_replace('/\{\([^.{}]*\)\+\}/', 'replace-string', $input); 

它会匹配{(abc)-}{(abc)+}事情。