[Solved] Remove the end of a string with a varying string length using PHP


You can use a regex like this:

-\d+x\d+(\.\w+)$

Working demo

enter image description here

The code you can use is:

$re = "/-\\d+x\\d+(\\.\\w+)$/"; 
$str = "http://website.dev/2014/05/my-file-name-here-710x557.png"; 
$subst="\1"; 

$result = preg_replace($re, $subst, $str, 1);

The idea is to match the resolution -NumbersXNumbers using -\d+x\d+ (that we’ll get rid of it) and then capture the file extension by using (\.\w+)$ using capturing group. Check the substitution section above.

6

solved Remove the end of a string with a varying string length using PHP