Zend - The PHP Company




Miscellaneous

Add Code


Replacement function with array support.  

Type: code fragment
Added by: inter
Entered: 15/08/2000
Last modified: 08/12/1999
Rating: - (fewer than 3 votes)
Views: 4476
To replace in $str all the [some] with the value in $some,
and with the {some} with the $art["some"].
With support for more then 1 {} of [] on 1 line.

If you have some better source for this, please mail me....


<?php
    
function replace($str)
    {
      global 
$art;

      if(
$nr1=strpos($str,'{'))  // a database fields
      
{
        
$tr=substr($str,0,$nr1);  
        
$nr2=strpos($str,'}',$nr1); 
        
$tr.=$art[substr($str,$nr1+1,$nr2-$nr1-1)];
        
$tr.=substr($str,$nr2+1);
        return 
replace($tr); // and maybe some more to do
      
}
      elseif(
$nr1=strpos($str,'[')) // a link or something
      
{
        
$tr=substr($str,0,$nr1);  
        
$nr2=strpos($str,']',$nr1); 
        
$tr.=$GLOBALS[substr($str,$nr1+1,$nr2-$nr1-1)];
        
$tr.=substr($str,$nr2+1);
        return 
replace($tr); // and maybe some more to do
      
}
      else
      {
        return 
$str// nothing to do.
      
}
    }
?>


Usage Example


Source:
<?
  $art
["name"]="Elvis";
  
replace("Hi [name] how are you?");
?>

Output:
Hi Elvis how are you?


Rate This Script





Search



This Category All Categories