Issue
I'm working with PHP and a JSON-based API that returns 2 fields with values for property photos:
- photo_url
- 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:
- The 4th level directory which specifies property ID (In the example above -- 102)
- 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.
Solution
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);
Answered By - Dmitry Egorov Answer Checked By - Katrina (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.