-1

I have the following url:

http://distilleryimage3_s3_amazonaws_com/8af11cdcf11e286b022000ae90285_7_jpg

I want to replace the _ with .. However, the _7 at the end should be kept - it is not a dot.

So, basically it should look like:

http://distilleryimage3.s3.amazonaws.com/8af11cdcf11e286b022000ae90285_7.jpg

If I use str_replace it will replace all the _ but I need to keep that one there. How can I do this?

anubhava
  • 761,203
  • 64
  • 569
  • 643
TiagoSouEu
  • 153
  • 1
  • 1
  • 5
  • 2
    Where are you getting this input from? – Niet the Dark Absol Sep 09 '13 at 16:38
  • Why don't you replace up to the first single '/' slash. Anything following the slash keep. Once complete, start from the end and replace the single _ with a . – bblincoe Sep 09 '13 at 16:38
  • Check out the solution to this question: http://stackoverflow.com/questions/8510223/php-str-replace-with-a-limit-param - You could use `substr_count` to get the total occurrences and subtract 1 to get your limit – Brendan Bullen Sep 09 '13 at 16:40
  • 3
    How did the URL get mangled? How about you, like, not replace the dots with underscores in the first place. – John Kugelman Sep 09 '13 at 16:43
  • Is this only going to be used once? If not, how can you be sure the exact same underscore will need to be kept every time? – trpt4him Sep 09 '13 at 16:45

4 Answers4

2

Use this (corrected now!)

<?php
$subject = 'http://distilleryimage3_s3_amazonaws_com/8af11cdcf11e286b022000ae90285_7_jpg';
$pattern = '/(_)(?!\d_jpg)/';


var_dump(preg_replace($pattern, '.', $subject));

This outputs

http://distilleryimage3.s3.amazonaws.com/8af11cdcf11e286b022000ae90285_7.jpg
ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • 3
    How about... if it is a random number rather than 7 placed at the end? – TiagoSouEu Sep 09 '13 at 16:46
  • this would only fix this SPECIFIC string. OP wants a generalized solution for all possible AWS urls. – Marc B Sep 09 '13 at 16:48
  • @TiagoSouEu It should work now (but it would fail in some cases, of course). – ComFreek Sep 09 '13 at 16:49
  • @TiagoSouEu I made another update, the pattern replaces all underscores except underscores followed by a digit, another underscore and 'jpg'. Does this meet your requirements? – ComFreek Sep 09 '13 at 16:55
  • 1
    There isn't group `\2` captured by your regex. – Toto Sep 09 '13 at 17:32
  • @TiagoSouEu - Just because it works now, doesn't mean it will work in a future variation. –  Sep 09 '13 at 18:06
2

Here's a solution that doesn't use regex (works for all numbers, so this won't fail if the number is different from 7; it doesn't have to be a number -- a string works, too):

 <?php

$haystack = 'http://distilleryimage3_s3_amazonaws_com/8af11cdcf11e286b022000ae90285_7_jpg';

//replacing all '_' with '.'
$haystack = str_replace('_', '.', $haystack);

//finding second last occurence of '.'
$n = strrpos($haystack, '.', strrpos($haystack, '.') - strlen($haystack) - 1);

//replacing the nth character to '_'
$haystack[$n] = '_';

echo $haystack;

Output:

http://distilleryimage3.s3.amazonaws.com/8af11cdcf11e286b022000ae90285_7.jpg

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • What if there are other dots `.` in the url, at the end? – randunel Sep 09 '13 at 17:49
  • @randunel: The domain extension doesn't matter. And I don't see how it's possible to have multiple dots for image URLs. – Amal Murali Sep 09 '13 at 17:52
  • haven't you ever seen a file named `my.super.cat.jpg`? It's possible. And I can serve anything through any url, including `example.com/my.super.dotted.endpoint` – randunel Sep 09 '13 at 18:22
  • @randunel: Yeah, it'd fail in that case, but the OP hasn't mentioned about it, so I just went with a general solution, that'd work for *most* of the URLs. It's always possible to find exceptions ;) – Amal Murali Sep 09 '13 at 18:33
1

You can use this negative lookahead based regex code:

$s='http://distilleryimage3_s3_amazonaws_com/8af11cdcf11e286b022000ae90285_7_jpg';
$repl = preg_replace('/_(?![^_]*_[^_]*$)/', '.', $s);
//=> http://distilleryimage3.s3.amazonaws.com/8af11cdcf11e286b022000ae90285_7.jpg
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

The final _ in a string that is not itself.

 _(?=[^_]+$)

Edit: my assumption is that this question needs specific knowledge of file extensions to answered correctly.