Created
November 22, 2020 10:09
-
-
Save guangrei/fdcdcf22c8412058e4a602da4508f016 to your computer and use it in GitHub Desktop.
php word replace , online demo https://ideone.com/94EjGy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function str_replace_word($cari, $ganti, $kalimat) | |
{ | |
$cek = strpos($kalimat, " "); | |
if ($cek != false) { | |
$token = explode(" ", $kalimat); | |
$map = array_map(function($val) use ($cari, $ganti) | |
{ | |
return $val === $cari ? $ganti : $val; | |
}, $token); | |
return implode(" ", $map); | |
} | |
return $kalimat === $cari ? $ganti : $kalimat; | |
} | |
$kalimat = "nama akun jono adalah @jono123.\n"; | |
echo $kalimat; | |
echo str_replace("jono", "budi", $kalimat); | |
echo str_replace_word("jono", "budi", $kalimat); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment