badcoder
badcoder

Reputation: 3834

PHP regex for image name with numbers

I have images with names such as:

img-300x300.jpg
img1-250x270.jpg

These names will be stored in a string variable. My image is in Wordpress so it will be located at e.g.

mywebsite.com/wp-content/uploads/2012/11/img-300x300.jpg

and I need the string to be changed to

mywebsite.com/wp-content/uploads/2012/11/img.jpg

I need a PHP regular expression which would return img.jpg and img1.jpg as the names.

How do I do this?

Thanks

Addition

Sorry guys, I had tried this but it didn't work

$string = 'img-300x300.jpg'
$pattern = '[^0-9\.]-[^0-9\.]';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);

Upvotes: 1

Views: 365

Answers (7)

zx81
zx81

Reputation: 41838

That's an interesting question, and since you are using php, it can be nicely solved with a branch reset (a feature of Perl, PCRE and a few other engines).

Search: img(?|(\d+)-\d{3}x\d{3}|-\d{3}x\d{3})\.jpg

Replace: img\1.jpg

The benefit of this solution, compared with a vague replacement, is that we are sure that we are matching a file whose name matches the format you specified.

Upvotes: 0

You can do this using PHP native functions itself.

<?php

function genLink($imagelink)
{
$img1 = basename($imagelink);
$img = substr($img1,0,strrpos($img1,'-')).substr($img1,strpos($img1,'.'));
$modifiedlink = substr($imagelink,0,strrpos($imagelink,'/'))."/".$img;
return $modifiedlink;
}

echo genLink('mywebsite.com/wp-content/uploads/2012/11/flower-img-color-300x300.jpg');

OUTPUT :

mywebsite.com/wp-content/uploads/2012/11/flower-img-color.jpg

Upvotes: 2

sshashank124
sshashank124

Reputation: 32189

You can do that as:

(img\d*)-([^.]*)(\..*)

and \1\3 will contain what you want:

Demo: http://regex101.com/r/vU2mD4

Or, replace (img\d*)-([^.]*)(\..*) with \1\3

Upvotes: 1

MonkeyZeus
MonkeyZeus

Reputation: 20737

As long as there is only one - and one . then explode() should work great for this:

<?php

// array of image names
$images = array();
$images[] = 'img-300x300.jpg';
$images[] = 'img1-250x270.jpg';

// array to store new image names
$new_names = array();

// loop through images
foreach($images as $v)
{
    // explode on dashes
    // so we would have something like:
    // $explode1[0] = 'img';
    // $explode1[1] = '300x300.jpg';
    $explode1 = explode('-',$v);

    // explode the second piece on the period
    // so we have:
    // $explode2[0] = '300x300';
    // $explode2[1] = 'jpg';
    $explode2 = explode('.',$explode1[1]);

    // now bring it all together
    // this translates to
    // img.jpg and img1.jpg
    $new_names[] = $explode1[0].'.'.$explode2[1];
}

echo '<pre>'.print_r($new_names, true).'</pre>';

?>

Upvotes: 0

Павел Иванов
Павел Иванов

Reputation: 1154

(.[^\-]*)(?:.[^\.]*)\.(.*)

group 1 - name before "-"

group 2 - extension. (everything after ".")

Upvotes: 0

aelor
aelor

Reputation: 11116

search : \-[^.]+

replace with : ''

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59232

May be this?

(\w+)-[^.]+?(\.\w+)

The $1$2 will give you what you want.

Upvotes: 0

Related Questions