-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?

5
  • 2
    Where are you getting this input from? Sep 9, 2013 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 9, 2013 at 16:38
  • Check out the solution to this question: stackoverflow.com/questions/8510223/… - You could use substr_count to get the total occurrences and subtract 1 to get your limit Sep 9, 2013 at 16:40
  • 3
    How did the URL get mangled? How about you, like, not replace the dots with underscores in the first place. Sep 9, 2013 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 9, 2013 at 16:45

4 Answers 4

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
6
  • 3
    How about... if it is a random number rather than 7 placed at the end?
    – TiagoSouEu
    Sep 9, 2013 at 16:46
  • this would only fix this SPECIFIC string. OP wants a generalized solution for all possible AWS urls.
    – Marc B
    Sep 9, 2013 at 16:48
  • @TiagoSouEu It should work now (but it would fail in some cases, of course).
    – ComFreek
    Sep 9, 2013 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 9, 2013 at 16:55
  • 1
    There isn't group \2 captured by your regex.
    – Toto
    Sep 9, 2013 at 17:32
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!

4
  • What if there are other dots . in the url, at the end?
    – randunel
    Sep 9, 2013 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. Sep 9, 2013 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 9, 2013 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 ;) Sep 9, 2013 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
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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.