Last active
August 29, 2015 14:19
-
-
Save permatis/99e7e0d74484dfaa5b0d to your computer and use it in GitHub Desktop.
This function for replace multidotted with string in array.
This file contains 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 | |
//Example array subject | |
$array1 = [ | |
'Name', | |
': ...', | |
'Tab', | |
'Semarang, ........' | |
]; | |
//Example array replace | |
$array2 = [ | |
'Defri', | |
'15-04-2015' | |
]; | |
var_dump($array1); | |
var_dump($array2); | |
/** | |
* This function for replace multiple dot with values. | |
* For example like a letter. | |
* Minimal a dot is 3 or more than. | |
*/ | |
function replace_multidotted($array_subject, $array_replace) | |
{ | |
foreach ($array_subject as $k => $v) { | |
if(strpos($v, '...')){ | |
$values[] = $v; | |
foreach ($values as $key => $val) { | |
for ($i=0; $i < count($array_replace); $i++) { | |
$text[$k] = str_replace(str_repeat('.', substr_count($v, '.')), $array_replace[$key], $val); | |
} | |
} | |
}else { | |
$text[] = $v; | |
} | |
} | |
return $text; | |
} | |
var_dump(replace_multidotted($array1, $array2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment