...

View Full Version : str_ireplace function..



cyphix
09-06-2005, 12:07 PM
I'm using the below function in place of the "str_ireplace" function since I do not have PHP5 installed..



if ( !function_exists('str_ireplace') )
{
function str_ireplace ($search, $replace, $subject) // >> , &$count)
{
//Loop back until done if using a subject array
if ( is_array($subject) )
{
$array_keys = array_keys($subject);
while ( true )
{
// This is done here rather than in the while() statement because 0 evaluates as false
$key = array_pop($array_keys);

// If $key is null then there are no more subjects
if ( $key === NULL)
{
return $subject;
}

$subject[$key] = str_ireplace($search, $replace, $subject[$key]); // >> , $count);
}
return $subject;
}

// Loop back until done if using a search array
if ( is_array($search) )
{
//Only need to check this once
$is_array = is_array($replace);

while ( true )
{
$needle = array_pop($search);

// If needle is null then there are no more searches to process
if ( $needle === NULL )
{
return $subject;
}

// If no needle, don't even bother
// In PHP, empty(0) returns true, so the second check is needed
if ( empty($needle) && (string)$needle !== "0" )
{
continue;
}

if ( $is_array == true )
{
// If no replacements are left, use empty string instead
if ( count($replace) )
{
$replacement = array_pop($replace);
}
else
{
$replacement = '';
}
}
else
{
// If replacement is not an array, use the same value each time
$replacement = $replace;
}

$subject = str_ireplace($needle, $replacement, $subject); // >> , $count);
}
return $subject;
}

// Now begins the meat of the function
// Define some variables - compiler throws 'E_NOTICE undefined variable' if you don't
$newsubject = '';
$end = '';
$offset = 0;

// Only need to do this once
$lowerneedle = strtolower($search);
$lowersubject = strtolower($subject);

// Keep going until I say so
while ( true )
{
// $pos will either be the position of the needle (can be 0) or FALSE
$pos = strpos($lowersubject, $lowerneedle, $offset);
// Operator === also checks against types, 0 == FALSE, 0 !== FALSE
if ($pos === false)
{
// Tack on the rest
$end = substr($subject, $offset);
$newsubject .= $end;
return $newsubject;
}

// Get the data before the needle
$front = substr($subject, $offset, $pos);
// Append the next piece and the replacement
$newsubject .= $front . $replace;
// Set offset to search beyond needle next time
// This is important because the replacement may contain the needle
$offset = $pos + strlen($search);
// Pretty self explanatory
// >> $count++;
}

// This should never happen, but it's here in case
return $subject;
}
}


Now.... i am using this code for example..



$st = str_ireplace($bterms, "<b>$bterms</b>", $title);


..but if the word contained inside $bterms has the first letter uppercased this function always returns the result in all lowercase chars... I also tried to do it with preg_replace() but got the same result.

I know I could just do this afterwards..



$st = ucfirst($st);


..but I don't want to do that because the word may not always be uppercased in the first place..

Any solutions?

Thanks! :D

cyphix
09-06-2005, 01:26 PM
..but if the word contained inside $bterms has the first letter uppercased this function always returns the result in all lowercase chars... I also tried to do it with preg_replace() but got the same result.

Woops.... I made a mistake. I meant that if any of the found results in $title began with an uppercase character then the returned result would be in all lowercase seeing as whatever input it gets it displays that rather than the what wa sorginally inside $title..... it basically just displays what $bterms holds..... but I want ot to output the correct format of the found word inside of $title but can't work out how to do it? :confused:

marek_mar
09-06-2005, 05:04 PM
str_ireplace doesn't preserve the case. You would have to get the case from the string... It should be doable with ASCII...

marek_mar
09-06-2005, 06:04 PM
This should work.


<?php
$search = array('fooo', 'bar');
$replace = array('bar', 'foo');
$string = 'Some Fooo is better than BaRr';
print sstr_ireplace($search, $replace, $string);
function sstr_ireplace($search, $replace, $string)
{ // the extra s is for "special"
if(is_array($search))
{
$input = implode('|', $search);
}
else
{
$input = $search;
$search = array($search);
}
if(!is_array($replace))
{
$replace = array($replace);
}
// Let's get make a search => replace array so we won't have to array_search().
$map = array();
for($i = 0, $n = count($search), $m = count($replace); $i < $n; $i++)
{
$map[strtolower($search[$i])] = strtolower($replace[($i % $m)]);
}
$result = explode(chr(0), preg_replace('/(' . $input . ')/i', chr(0) . '\\1' . chr(0), str_replace(chr(0), '', $string)));
for($i = 0, $n = count($result); $i < $n; $i++)
{
if($i % 2)
{
$uc = $result[$i];
$lc = strtolower($result[$i]);
$result[$i] = $map[$lc];
for($j = 0, $m = strlen($uc); $j < $m; $j++)
{ // The if below hopes that indexes can be anything :)
if(!isset($result[$i]{$j}))
{ // No need to go on if $replace is shorter than $search.
break;
}
if(($uc{$j} != $lc{$j}))
{ // Different case. Testing showed that == is case sensitive... it can be done in another way if it is only on my PHPversion.
$result[$i]{$j} = strtoupper($result[$i]{$j});
}
}
}
}
return implode('', $result);
}
?>

cyphix
09-10-2005, 12:38 AM
Thanks a lot Marek but it didn't work. :(

marek_mar
09-10-2005, 03:34 AM
It didn't? Have anything else to say? It works for me.

dumpfi
09-10-2005, 03:34 PM
Try this:

<?php
if(!function_exists('str_ireplace')) {
function str_ireplace($search, $replace, $subject, &$count = 0) {
if(is_array($subject)) {
foreach($subject as &$subj) {
$subj = str_ireplace($search, $replace, $subj, $count);
}
return $subject;
}
if(is_array($search)) {
$isReplArr = is_array($replace);
if(!$isReplArr) $replacement = $replace;
while(($needle = array_pop($search)) !== NULL) {
if($isReplArr) {
if(empty($replace)) $replacement = '';
else $replacement = array_pop($replace);
}
$subject = str_ireplace($needle, $replacement, $subject, $count);
}
return $subject;
}
if(($pos = stripos($subject, $search)) === FALSE) return $subject;
++$count;
return substr($subject, 0, $pos).$replace.substr($subject, $pos + strlen($search));
}
}
?>dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum