Regular Expression-PHP, how to write regular expression replacement?

Source: Internet
Author: User
There are strings: & #039; sdfUjer [ema14] XX [emb15], 43 you 56 ^ & amp; % ^ % I [emc7] & #039; [ema14], [emb15], and [emc7] are the content to be replaced. & #039; [em & #039; start with & #039;] & #039; end with a combination of letters and numbers in the middle. replace all those that match the structure with & amp; lt; a14 & amp; gt;, & amp ;... there are strings: 'sdfujer [ema14] XX [emb15], 43 you 56 ^ & % ^ % I [emc7]'

[Ema14], [emb15], and [emc7] are the content to be replaced,

'[Em', ']', with a combination of letters and numbers in the middle

Replace all and all that conform to this structure ,,

Finally, we should get the following string: 'sdfujerxx , 43 you 56 ^ & % ^ % Me ]'

---------- Update ------------
Downstairs
Wen Jing [1]: http://segmentfault.com/u/wemlin
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 ^ & % ^ % I [emc7]'

[Ema14], [emb15], and [emc7] are the content to be replaced,

'[Em', ']', with a combination of letters and numbers in the middle

Replace all and all that conform to this structure ,,

Finally, we should get the following string: 'sdfujerxx , 43 you 56 ^ & % ^ % Me ]'

---------- Update ------------
Downstairs
Wen Jing [1]: http://segmentfault.com/u/wemlin
Can be replaced correctly,
Preg_replace ('/\ [em ([a-zA-Z] + \ d +) \]/', '<$1>', $ str );

Lz forgive me for not using PHP for a long time. Str_replace function or something.

Considering that regular expressions are basically common. From the js perspective, let's take a look at lz.

Var str = 'sdfujer [ema14] XX [emb15], 43 you 56 ^ & % ^ % I [emc7] '; var regex =/\ [em ([a-zA-Z] + \ d +) \]/g; var result = str. replace (regex, '<$1>'); console. log (result = 'sdfujerxx
  
   
, 43 you 56 ^ & % ^ % Me
   
    
] ');
   
  

$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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.