SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace and string traversal

    Hi everyone,

    Not having much luck trying to get a string replacement script working.

    PHP Code:
    $str 'The quick ? fox ? over the ? ?';
    $values = array('brown','jumps','lazy','dog');

    $str str_replace('?',$values,$str);

    echo 
    $str
    I want to traverse $str and swap out the ? characters, replacing them with the corresponding value in the $values array. When I run this script, the output reads "The quick Array fox Array over the Array Array".

    Is there a way of doing this without having to resort to regular expressions?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I'm sure there is a much better way, but please bear in mind I haven't really woke up yet!



    PHP Code:
    <?php

    $sString 
    'The quick ? fox ? over the ? dog';

    $aReplacements = array('brown','jumped','lazy');

    for (
    $iStringIndex 0;$iStringIndex <= strlen($sString);$iStringIndex++){
        if(
    $sString[$iStringIndex] == '?'){
            
    $sCompiledString .= $aReplacements[(is_null($iArrayIndex) ? $iArrayIndex)];
            
    $iArrayIndex++;
            continue;
        }
        
    $sCompiledString .= $sString[$iStringIndex];
    }

    echo 
    $sCompiledString#The quick brown fox jumped over the lazy dog

    ?>
    Last edited by AnthonySterling; Dec 31, 2008 at 06:43. Reason: Well, it just didn't work.. :-(
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Another, inelegant solution;
    PHP Code:
    // say you already know how many replacements
    $str 'The quick # fox # over the # #';
    $values = array('brown','jumps','lazy','dog');

    $patterns array_fill0,"/#/" ) ;  

    echo 
    preg_replace($patterns$values$str1) . '<hr>';

    //The quick brown fox jumps over the lazy dog

    // OR say you don't know how many replacements
    $str 'The quick # fox # over the # #';
    $values = array('brown','jumps','lazy','dog');
    $cnt  preg_match_all"/#/" $str $c ); 

    $patterns array_fill0$cnt ,"/#/" ) ;  
    echo 
    preg_replace($patterns$values$str1);

    //The quick brown fox jumps over the lazy dog 


    Edit:


    Actually, this is better because it degrades ok if values doesn't match the string holders #

    // you don't know how many replacements
    $str = 'The quick # fox # over the # # ';
    $values = array('brown','jumps','lazy','dog');

    $patterns = array_fill( 0, count($values) ,"/#/" ) ;

    echo preg_replace($patterns, $values, $str, 1);

    //The quick brown fox jumps over the lazy dog

  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    reset($values);

    $str preg_replace_callback(
        
    '/ \?/',
        
    create_function(
            
    '$string',
            
    'global $values;' .
            
    'list($key, $value) = each($values);' .
            
    'return " $value";'
        
    ),
        
    $str
    ); 
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use &#37;s instead of ?
    PHP Code:
    $sString 'The quick %s fox %s over the %s dog';
    $aReplacements = array('brown','jumped','lazy');
    $str vsprintf($sString,$aReplacements
    or
    PHP Code:
    $sString 'The quick ? fox ? over the ? dog';
    $aReplacements = array('brown','jumped','lazy');
    $str vsprintf(str_replace('?','%s',$sString),$aReplacements
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    str_replace doesn't really work with a string for the search and array for the replace. You could use an array for the search but they'd have to be different (not all a ?) but really that would be trying to recreate a built in functionality of the formatted strings.

    Using vsprintf is the best solution as posted above. Plus, you're less likely to have %s occur in a string than a question mark (depending on what the data is I suppose).
    - Robert

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark Baker View Post
    Use %s instead of ?
    PHP Code:
    $sString 'The quick %s fox %s over the %s dog';
    $aReplacements = array('brown','jumped','lazy');
    $str vsprintf($sString,$aReplacements
    Very nice Mark, something else new to me today!

    It makes me really want to hand my head in shame, maybe I got away with it due to the "it's early" excuse...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •