1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Insert a string in the middle of another string ?

Discussion in 'PHP' started by fatabbot, Nov 21, 2006.

  1. #1
    Hi,
    SEMrush

    Does anyone know how to place a string into another string on a specified position ?
    Like this for example:

    $stringA = "testfile.jpg";


    DO SOMETHING to insert '_new' in $stringA


    $stringA = "testfile_new.jpg";
     
    fatabbot, Nov 21, 2006 IP
    SEMrush
  2. mani

    mani Peon

    Messages:
    679
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If it matters for inserting it in same string that have a 3 character extension(.jpg,.exe,.bmp etc) then following might be helpful

    
    <?php
    $stringA="filename.jpg";
    $stringB="_new";
    $length=strlen($stringA);
    $temp1=substr($stringA,0,$length-4);
    $temp2=substr($stringA,$length-4,$length);
    echo $temp1.$stringB.$temp2;     // Displays  filename_new.jpg
    ?>
    
    Code (markup):
     
    mani, Nov 21, 2006 IP
  3. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #3
    $string = "picture.jpg";
    $new = "_new";
    $pos = ".jpg";
    echo str_replace($pos, $new.$pos ,$string);
    Code (markup):
     
    falcondriver, Nov 21, 2006 IP
  4. boccio

    boccio Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    // $insertstring - the string you want to insert
    // $intostring - the string you want to insert it into
    // $offset - the offset
    
    function str_insert($insertstring, $intostring, $offset) {
       $part1 = substr($intostring, 0, $offset);
       $part2 = substr($intostring, $offset);
      
       $part1 = $part1 . $insertstring;
       $whole = $part1 . $part2;
       return $whole;
    }
    
    PHP:
     
    boccio, Nov 21, 2006 IP
  5. mani

    mani Peon

    Messages:
    679
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    boccio has given you a better solution, try his code
     
    mani, Nov 21, 2006 IP
  6. crazybjörn

    crazybjörn Peon

    Messages:
    270
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Problem with his function is that you always need to know how long the filename is, if he would do a reverse offset(from right to left) then you could just set offset to 4 always.
     
    crazybjörn, Nov 21, 2006 IP
  7. falcondriver

    falcondriver Well-Known Member

    Messages:
    963
    Likes Received:
    47
    Best Answers:
    0
    Trophy Points:
    145
    #7
    whats wrong with my solution? here as a function again:
    function insertstring($string, $new, $pos) {
       return  str_replace($pos, $new.$pos ,$string);
    }
    Code (markup):
     
    falcondriver, Nov 21, 2006 IP
    greatlogix, fatabbot and YIAM like this.
  8. YIAM

    YIAM Notable Member

    Messages:
    2,486
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #8
    falcondriver is suggestion seems more useful then others.
     
    YIAM, Nov 21, 2006 IP
  9. crazybjörn

    crazybjörn Peon

    Messages:
    270
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That is fine, and works like it should, and can be kept constant without adjusting the offset like in the other one.
     
    crazybjörn, Nov 21, 2006 IP
  10. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    This thread solved a big problem for me...

    Its really difficult to choose from which string function to use in PHP...
    Lots of them.
     
    weknowtheworld, Nov 21, 2006 IP
  11. fatabbot

    fatabbot Peon

    Messages:
    559
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks guys
     
    fatabbot, Nov 23, 2006 IP
  12. foomagoo

    foomagoo Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #12
    Use substr_replace.
     
    foomagoo, Jan 10, 2012 IP
  13. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    51
    Best Answers:
    33
    Trophy Points:
    150
    #13
    This should work no matter what the file name/extension is or how long they are:

    
    $fileName = 'filename.jpeg'; // or .gif, .jpg, etc..
    $ext = '.' . end(explode('.', $fileName));
    $newName = str_replace($ext, '_new' . $ext, $fileName);
    
    PHP:
     
    ThePHPMaster, Jan 10, 2012 IP