Search code examples
phpreplacesubstringellipsis

Limit a filename character by the middle in PHP


I have a filename stored as 25507647_377205372719383_8438629006006905602_n.jpg and would like it to be display as 255076...905602_n.jpg


Solution

  • substr_replace() for the win.

    substr_replace — Replace text within a portion of a string

    Depending on your design logic, you can offer a positive or negative length parameter.

    Code: (Demo)

    $file = "25507647_377205372719383_8438629006006905602_n.jpg";
    echo substr_replace($file, '...', 6, 32);
    echo "\n";
    echo substr_replace($file, '...', 6, -12);
    

    Output:

    255076...905602_n.jpg
    255076...905602_n.jpg