0

In JSON string I need to replace inconsistent values in multiple instances

String format to replace within each instance

inconsistent value 252/2022/08 to be replaced with consistent value 178/2023/03

{"url":"/wp-content/uploads/sites/252/2022/08/banner-rental.jpg",

Another instance may be. Note the difference with 06

{"url":"/wp-content/uploads/sites/252/2022/06/banner-swx.jpg",

The replacement value is passed by WordPress wp_upload_dir()['url'] which is /wp-content/uploads/sites/178/2023/03 stored in var $replace

method tried

preg_replace('#/wp-content/uploads/sites/(.*?)/#i',$replace,$jsondata);

The result is close but of course the stop point is at the first slash after sites/. I can't define the process to find and set the last slash as the end point.

{"url":"/wp-content/uploads/sites/178/2023/032022/06/banner-swx.jpg",

How can I find and set the last slash before the image name, as the ending point for the search?

2
  • You should use a pattern, not .*. It is always a bad date? Maybe regex101.com/r/MOAiFK/1 works Commented Mar 13, 2023 at 18:42
  • Literally just drop the ? from the pattern. Commented Mar 13, 2023 at 20:42

1 Answer 1

0

This pattern returned the correct result, don't know why but it works as desired.

$data = '{"url":"/wp-content/uploads/sites/252/2022/06/banner-swx.jpg","url":"/wp-content/uploads/sites/252/2022/09/banner-rental.jpg"}';
$replace = wp_upload_dir()['url'];
preg_replace('#/wp-content/uploads/sites/(.*?)[^/]{4}[/$][^/]{2}#i',$replace,$data);
Sign up to request clarification or add additional context in comments.

5 Comments

Provided regex 17 hours didn't work? You've written capture everything until a non / then look for another non ., then look for another non . 4 more times, then a / or $ (not sure why you're looking for a $). then 2 more times a non /. [^/][^/][^/][^/] is the same as [^/]{4} but even that is a very loose regex. Should realllly define a pattern.
I did not follow the link you posted. If you have or had a solution, post it as an answer so others can benefit, and if it does what I need, I'll check it as accepted.
The link shows everything, click it. Or if by didn't follow you meant understand what the pages is showing.. the top is the regex, middle is your string, bottom is substitution. You haven't provided enough details to define a good regex so I can't post an answer. My best guess is above.
@user3783243 I did not click the link before and still haven't. Not sure how much more detail you need. That you posted a solution at another site suggests you understand the inquiry. Just post the answer at this website so others can gain from your knowledge.
Is there a pattern? Is /wp-content/uploads/sites/#/#/$/*.* consistent? Can numbers vary, extension be optional, etc? You only provided 1 example.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.