Skip to content

Instantly share code, notes, and snippets.

@yehchge
Last active November 10, 2016 07:35
Show Gist options
  • Save yehchge/f0697c713411030f14955f5c2fad16b4 to your computer and use it in GitHub Desktop.
Save yehchge/f0697c713411030f14955f5c2fad16b4 to your computer and use it in GitHub Desktop.
重複的關鍵字只替換一次
<?php
// 重複的關鍵字只替換一次
$str = "http://www.school.com/asdf2m/asdf/dd1234acfgesger/asdf/asdf/屁屁";
$a = array('/(AC)/i','/(ac)/i','/(dd1234)/i','/(dd12)/i','/(er)/i','/(屁屁)/i');
echo my_replace($a, $str);
function my_replace($aKeyword, $sString){
$aTmp = preg_replace($aKeyword, "[indent]$1[/indent]" , $sString);
return parseTagsRec($aTmp);
}
function parseTagsRec($input) {
$regex = '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#';
if (is_array($input)) {
$input = "<span style=\"background:#fff203;\"><b>".$input[1]."</b></span>";
}
return preg_replace_callback($regex, 'parseTagsRec', $input);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment