Zend - The PHP Company




Miscellaneous

Add Code


str_swap  

Type: code fragment
Added by: stephenv
Entered: 12/06/2001
Last modified: 07/12/2000
Rating: - (fewer than 3 votes)
Views: 3743
I was kind of hoping to find a function where I can swap values in a string, but I couldn't find one, so I wrote one, here it is


<?php
function str_swap($swp1,$swp2,$str)
     {
     
srand ((double) microtime() * 1000000);
     for(
$i=0;$i<32;$i++) $tmp .= rand(0,9);
     
str_replace($swp1,$tmp.$swp1.$tmp,$str);
     
str_replace($swp2,$swp1,$str);
     
str_replace($tmp.$swp1.$tmp,$swp2,$str);
     return 
$str;
     }
?>


Usage Example


$string = 'Bob went out with Sally.'; 
echo str_swap('Bob','Sally',$string);

RESULT:
Sally went out with Bob.


Rate This Script





Search



This Category All Categories