Using arrays in string replacement... 
Author Message
 Using arrays in string replacement...

$test = array('MAMA' => 'papa',
   'SISTER' => 'brother');

I want this array to be used for replaceing inside a string, the case shouldn't matter but
it should adopt the case of the source (so replace Mama  by Papa and SiStEr by BrOtHer).
I'm not sure how to start without using a loop which would make the script slow...

--
www.wazzoo.net



Tue, 26 Apr 2005 05:22:10 GMT  
 Using arrays in string replacement...

Quote:

> $test = array('MAMA' => 'papa',
>    'SISTER' => 'brother');

> I want this array to be used for replaceing inside a string, the case
> shouldn't matter but
> it should adopt the case of the source (so replace Mama  by Papa and
> SiStEr by BrOtHer). I'm not sure how to start without using a loop which
> would make the script slow...

It would be very simple except for your last requirement of adjusting the
case - that's the only part which would be slow. preg_replace() already
does the rest of it, and you could probably get tricky with strtr() to do
the CaSE MAtChING bit.

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Tue, 26 Apr 2005 17:32:25 GMT  
 Using arrays in string replacement...



Quote:

> $test = array('MAMA' => 'papa',
>    'SISTER' => 'brother');

> I want this array to be used for replaceing inside a string, the case

shouldn't matter but

Quote:
> it should adopt the case of the source (so replace Mama  by Papa and
SiStEr by BrOtHer).
> I'm not sure how to start without using a loop which would make the script
slow...

> --
> www.wazzoo.net

It is not pretty, but it works:

<?

$test = array('MAMA' => 'papa', 'SISTER' => 'brother', 'SIBLING' =>
'ancestor','GRANDMOTHER'=>'Aunt');

$string = "Mama loves Sister like SibLing loves Grandmother" ;

foreach ($test as $key => $value) {
 if (eregi($key, $string, $match)) {
  $repl = "" ;
  for ($i = 0; $i < strlen($match[0]); $i++) {
   $char = $match[0][$i] ;
   if ($match[0][$i] == strtolower($char)) {
    $repl .= strtolower($test[$key][$i]) ;
   } else if ($match[0][$i] == strtoupper($char)) {
    $repl .= strtoupper($test[$key][$i]) ;
   } else {
    $repl .= $test[$key][$i] ;
   }
  }
  $repl = strlen($test[$key]) > strlen($repl) ?
$repl.substr($test[$key],strlen($repl)) : $repl ;
  $string = str_replace($match[0],$repl, $string) ;
 }

Quote:
}

echo $string ;

?>

HTH

Regards, JW



Thu, 28 Apr 2005 06:57:03 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. problems using % in strings with %(var)s replacements

2. Array to String and String to Array

3. convert 2d array to 1d array without using shift registers and build array

4. awk newbie needs assistance with string replacement

5. urgent request for comments on a string replacement method

6. literal \0 in String#gsub replacement

7. string replacement in C interface ?

8. string instruction replacements for Pentium pro/II

9. Multiple string replacement...

10. string parameter replacement question

11. Evaluated replacement strings

12. String replacements

 

 
Powered by phpBB® Forum Software