1

File Name

cars-lights-neon-1920x1080.jpg
cars-lights-neon-3840x2160.jpg
cars-lights-neon-800x600.jpg

What I Need is

cars-lights-neon.jpg

What I Tried

$name="cars-lights-neon-1920x1080.jpg";
$newname =   substr($name, 0, -4); //removing.jpg
// Real name
$rev= strrev("$newname"); // outputs "0801x0291-noen-sthgil-srac"
$revok= strstr($rev, '-'); //outputs "-noen-sthgil-srac"
$neutral = strrev("$revok"); //outputs "cars-lights-neon-"
$neutral = substr($neutral, 0, -1); //outputs "cars-lights-neon"

$filename = $neutral.".jpg"; //outputs "cars-lights-neon.jpg";

This Code Also Works fine.. But it can be done in Simple steps..So What i am Asking is for better code..

//File Name and Resolution is coming from database so it is not same every time.. Thnx

2 Answers 2

2

Simply use preg_replace function of PHP like as

$name = "cars-lights-neon-1920x1080.jpg";
echo preg_replace('/(-\d+x\d+)/','',$name);//cars-lights-neon.jpg

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

One Thing More cars-lights-neon-3840x2160.jpg I Also Need Width and Height<br> $width = "3840"; $height ="2160";
Simply use preg_match('/-(\d+)x(\d+)/',$name,$m); print_r($m); Check This
1

You can use preg_replace:

$string = 'cars-lights-neon-1920x1080.jpg';
$filename = preg_replace('/-[0-9]{3,4}x[0-9]{3,4}/', '', $string);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.