SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    replace one time

    Code:
    $myVar="abcd ABCD abcd";
    
    $myVar1=str_ireplace("b", "",$myVar);
    
    result1
    
    acd ACD acd
    
    $myVar2=str_replace("b", "",$myVar);
    
    result2
    
    acd ABCD acd
    The code above prouduces the result above.

    I like to replace "b" just one time.
    The would-be code below doesn't work correctly, but I hope it show what I want.
    Code:
    would-be code
    
    $myVar3=str_ireplace("b", "",$myVar, 1);
    
    target result
    
    acd ABCD abcd
    How can I get my target result above?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You can use strpos then do a substr_replace

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The following trials seems not to work.
    Code:
    trial1
     
    substr_replace(2, $myVar)
    
    trial2
     
    substr_replace("b", $myVar,2)

  4. #4
    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)
    Code:
    <?php
    $string = 'abcdef abcdef abcdef';
    
    
    if(false !== ($index = strpos($string, 'b'))){
    	$string[$index] = null;
    }
    
    
    echo $string;
    @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
  •