There are strings: ' sdfujer[ema14]xx[emb15],43 you 56^&%^% me [EMC7]] []]
where [EMA14],[EMB15],[EMC7], is the content to be replaced,
' [em ' begins, '] ' ends, the middle consists of letters plus numbers
Replace all of them with this structure,
Should finally get such a string: ' Sdfujerxx , 43 you 56^&%^% me ]] []]
----------Update------------
Downstairs
Wen Lin [1]:http://segmentfault.com/u/wemlin
The answers can be replaced correctly,
Preg_replace ('/\[em ([a-za-z]+\d+) \]/', ' <$1> ', $str);
Reply content:
There are strings: ' sdfujer[ema14]xx[emb15],43 you 56^&%^% me [EMC7]] []]
where [EMA14],[EMB15],[EMC7], is the content to be replaced,
' [em ' begins, '] ' ends, the middle consists of letters plus numbers
Replace all of them with this structure,
Should finally get such a string: ' Sdfujerxx , 43 you 56^&%^% me ]] []]
----------Update------------
Downstairs
Wen Lin [1]:http://segmentfault.com/u/wemlin
The answers can be replaced correctly,
Preg_replace ('/\[em ([a-za-z]+\d+) \]/', ' <$1> ', $str);
LZ please forgive me for a long time useless PHP. Str_replace function or something.
Given that regular expressions are basically common. I answer from the angle of JS, LZ right vote a look at it.
var str = 'sdfUjer[ema14]XX[emb15],43你56^&%^%我[emc7]]]]]]';var regex = /\[em([a-zA-Z]+\d+)\]/g;var result = str.replace(regex , '<$1>');console.log(result === 'sdfUjerXX
,43你56^&%^%我
]]]]]');
$str = "[ema14]XXX[emb15]XXX[emc77]";$pattern = '/\[em([^\]]*)\]/';$resultArr = array();$index = 0;function replaceStr ($arr) {GLOBAL $resultArr;$resultArr[] = "<" . $arr[1] .">";}preg_replace_callback($pattern, 'replaceStr', $str);$str = implode("", $resultArr);