Skip to content

Instantly share code, notes, and snippets.

@robertopc
Forked from adhocore/str_ireplace_array.php
Created May 7, 2020 20:10
Show Gist options
  • Save robertopc/fdb802109a2f665a1207018b85f1a0d5 to your computer and use it in GitHub Desktop.
Save robertopc/fdb802109a2f665a1207018b85f1a0d5 to your computer and use it in GitHub Desktop.
A str_replace_array for PHP
<?php
/**
* A str_ireplace_array for PHP
*
* Case insensitive version of str_replace_array
* See https://wiki.php.net/rfc/cyclic-replace
*
* @author Jitendra Adhikari | adhocore <[email protected]>
*
* @param string $search The search string
* @param array $replace The array to replace $search in cyclic order
* @param string $subject The subject on which to search and replace
*
* @return string
*/
function str_ireplace_array($search, array $replace, $subject)
{
if (0 === $tokenc = substr_count(strtolower($subject), strtolower($search))) {
return $subject;
}
$string = '';
if (count($replace) >= $tokenc) {
$replace = array_slice($replace, 0, $tokenc);
$tokenc += 1;
} else {
$tokenc = count($replace) + 1;
}
foreach(preg_split('/'.preg_quote($search, '/').'/i', $subject, $tokenc) as $part) {
$string .= $part.array_shift($replace);
}
return $string;
}
// Usage:
// Subject with number of $search === count($replace)
var_dump(str_ireplace_array(
'x/z', # Search
[ 'c', 'f', 'i'], # Replace
'a b X/z d e x/Z g h X/Z j' # Subject
));
// Subject with number of $search < count($replace)
var_dump(str_ireplace_array(
'x/z', # Search
[ 'c', 'f', 'i'], # Replace
'a b X/z d e x/Z g h i j' # Subject
));
// Subject with number of $search > count($replace)
var_dump(str_ireplace_array(
'x/z', # Search
[ 'c', 'f', ], # Replace
'a b X/z d e x/Z g h X/Z j' # Subject
));
<?php
/**
* A str_replace_array for PHP
*
* As described in http://php.net/str_replace this wouldnot make sense
* However there are chances that we need it, so often !
* See https://wiki.php.net/rfc/cyclic-replace
*
* @author Jitendra Adhikari | adhocore <[email protected]>
*
* @param string $search The search string
* @param array $replace The array to replace $search in cyclic order
* @param string $subject The subject on which to search and replace
*
* @return string
*/
function str_replace_array($search, array $replace, $subject)
{
if (0 === $tokenc = substr_count($subject, $search)) {
return $subject;
}
$string = '';
if (count($replace) >= $tokenc) {
$replace = array_slice($replace, 0, $tokenc);
$tokenc += 1;
} else {
$tokenc = count($replace) + 1;
}
foreach(explode($search, $subject, $tokenc) as $part) {
$string .= $part.array_shift($replace);
}
return $string;
}
// Usage:
// Subject with number of $search === count($replace)
var_dump(str_replace_array(
'?', # Search
[ 'c', 'f', 'i'], # Replace
'a b ? d e ? g h ? j' # Subject
));
// Subject with number of $search < count($replace)
var_dump(str_replace_array(
'?', # Search
[ 'c', 'f', 'i'], # Replace
'a b ? d e ? g h i j' # Subject
));
// Subject with number of $search > count($replace)
var_dump(str_replace_array(
'?', # Search
[ 'c', 'f', ], # Replace
'a b ? d e ? g h ? j' # Subject
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment