Search code examples
phpsubstrstrlen

PHP remove characters after a specific string for a certain length


I have a string that is:

<p><img src="../filemanager/image.png?1476187745382"/></p> some text ... 

I would like to remove everything after a .png or .jpg when question mark occurs. The goal is to remove the timestamp added ?1476187745382 but not the "/></p> some text ...

Keeping in mind that the timestamp, will change and the what comes after the the image > will also be different.

I have looked at different solutions, but they all remove either the exact occurrence or everything after a certain character, which is not what I need to do.

This is what I have looked at:

  1. PHP remove characters after last occurrence of a character in a string
  2. Remove portion of a string after a certain character

Can someone point me to the right direction?


Solution

  • Not always needed, but a regex will do it:

    $string = preg_replace('/\?[\d]{13}/', '', $string);
    

    If the timestamp is not always 13 digits then replace the {13} with just a +.