0

I'm working with PHP and a JSON-based API that returns 2 fields with values for property photos:

  1. photo_url
  2. has_large

The photo_url is the regular sized photo URL.

Example: https://secure.example.org/Directory/AnotherDirectory/0008/102/12.jpg

If has_large = true, then we know a large photo exists, but the API doesn't return the specific URL.

However, from the API documentation, we know that if we prefix the image filename with a 'L', this gives us the correct large image URL:

Example: https://secure.example.org/Directory/AnotherDirectory/0008/102/L12.jpg

How can I programmatically prefix the filenames with the letter L, before writing them to the database (MySQL)?

Note: The URL structure is nearly the same for all photos.

The only parts of the image URLs that change for each property are:

  1. The 4th level directory which specifies property ID (In the example above -- 102)
  2. The filename itself, which is a maximum of 3 numbers (In the URL example above -- 12.jpg).

After hours of research on RegEx, preg_replace() and looking for similar solutions on StackOverflow, I'm still not sure how to accomplish this correctly using PHP.

Any help would be appreciated. Thanks in advance.

3 Answers 3

3

It seems you only need to prepend a file name (without path) with L. A file name without path is anything but / in at the end of string, which in regex looks ([^\/]+)$. Therefore the desired replacement function is:

$large_photo_url = preg_replace('/([^\/]+)$/', 'L$1', $photo_url);
2
  • Can you explain to me what the second parameter ($1) in the preg_replace is doing and how it represents the letter L?
    – CoastalPro
    May 16, 2017 at 15:50
  • 1
    @ju1985oh $1 is the first capture group (everything from the last / until the end of the string).
    – chris85
    May 16, 2017 at 16:13
1

Here we are using explode function, to split a string on / and then appending L at the very end value of that array.

Solution 1:

Try this code snippet here

<?php

$link="https://secure.example.org/Directory/AnotherDirectory/0008/102/12.jpg";
$portions=explode("/",$link);
$portions[count($portions)-1]="L".$portions[count($portions)-1];
print_r(implode("/", $portions));

Solution 2:

Regex: ([^\/]+)$

1. ([^\/]+) match all till /(not including this), here circle () braces will capture the first captured result in $1,

2. $ end of string.

Regex demo

<?php

$link="https://secure.example.org/Directory/AnotherDirectory/0008/102/12.jpg";
echo preg_replace("/([^\/]+)$/", 'L$1', $link);
0
0

For my case.. I am using this function to get the updated image url.

function thumb_url($image_url=''){

    $prefix = 'thumb';
    $photo_url = preg_replace('/([^\/]+)$/', "{$prefix}$1", $image_url); 

    return $photo_url;
}

credit goes to @Dmitry. I Just used his code to here.

Your Answer

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

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