There is a question of how to replace a particular string in a string. Example: Replace the string: ABC Fdab ertde FDA ABC FDD in ABC, where ABC is case-insensitive and replaced with the following string: Fdab ertde FDA FDD
At that time, I thought of two strategies preg_replace regular replacement, Preg_split split merge. Ah ah, but there is no manual at the hand, always have no courage to try, here show the code bar, it seems to be careful later. On the Code bar:
[PHP]
$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$rtn = Preg_replace ($pat, ", $str,-1);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;
$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$rtn = Preg_replace ($pat, ", $str,-1);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn; Please click Preg_replace to see the usage of the function. Showshowpreg_split It again:
[PHP]
$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$arr = Preg_split ($pat, $STR);
$rtn = Implode (' ', $arr);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;
$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$arr = Preg_split ($pat, $STR);
$rtn = Implode (' ', $arr);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;
Hehe, although this way is very stupid, but it is a way ah, but in the final analysis is the wording of the regular. Is there another way, thanks to manual in hand, Kung Fu, there is another kind of stupid method immediately:
[Plain]
$STR = ' ABC Fdab ertde FDA abc FDD ';
$tran = Array (' abc ' = = ', ' abc ' + = ');
$rtn = Strtr ($str, $tran);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;
$STR = ' ABC Fdab ertde FDA abc FDD ';
$tran = Array (' abc ' = = ', ' abc ' + = ');
$rtn = Strtr ($str, $tran);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;
This way some trickery, mainly use STRTR to evade the regular, deviate from others ' purpose, also is not a good method!
This time, the question of the regular is to reflect, although the regular learning a lot, also wrote some. Can always use the time to read the manual, constantly retry rewriting, not clear on the chest. Heart to it some timid, should later write more practice, truly familiar with this basic skills.
http://www.bkjia.com/PHPjc/477587.html www.bkjia.com true http://www.bkjia.com/PHPjc/477587.html techarticle There is a question of how to replace a particular string in a string. Example: Replace the string: ABC Fdab ertde FDA ABC FDD in ABC, where ABC is case-insensitive, replaced ...