Skip to content

Instantly share code, notes, and snippets.

@Hillzacky
Created October 18, 2020 19:36
Show Gist options
  • Save Hillzacky/e2ffd26653094c9dbbdc04c973a37305 to your computer and use it in GitHub Desktop.
Save Hillzacky/e2ffd26653094c9dbbdc04c973a37305 to your computer and use it in GitHub Desktop.
str_replace multiple pattern code string in php bulk replace string
<?php
function wordTruth ($text,$args=[]){
foreach($args as $k => $v){
$key[] = $k;
$value[]= $v;
}
return str_replace($key,$value,$text);
}
$name = "Akunk";
$gender = "gadis";
$text = "Hai [gender] kamu istimewa, kenalkan nama saya [nama].";
print_r( wordTruth($text,["[nama]"=>$name,"[gender]"=>$gender]) );
/**
Output:
Hai gadis kamu istimewa, kenalkan nama saya Akunk.
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment