Jump to content

Need help changing URL to image within body text to current page id using preg_replace


grimezy
 Share

Recommended Posts

Hi everyone, 

I have just updated my wife's website, and used the PW pages import/export tool to export all her blog posts.  Everything worked well except that the Page ID's are different, so images which are in ckeditor body field are pointing to their old page id's and not the new page id's. Causing error's when you try to change any image properties within the ckeditor, such as alignment etc... and no images within the body field are shown on the front end of the site.

Is there a step that I have missed somewhere? I did this step last, so I'm guessing all page id's were taken up, so the import used new page id's? 

Has anyone made a preg_replace() for URLs to just change image url strings to update to a new page id?

For example I need the following example:

<img alt="" src="/site/assets/files/10877/img_543.jpeg" />

changed to:

<img alt="" src="/site/assets/files/{$page->id}/img_543.jpeg" />

Thanks!

Link to comment
Share on other sites

On mobile here, but you can do something like that with a Textformatter Module. I remember one that restrict images, it is available in the modules directory. The author is @Martijn Geerts , maybe you can take this as a starting point or just „borough“ some code from it. 

Edit: 

the link to the module:

https://modules.processwire.com/modules/textformatter-image-interceptor/

 

  • Like 1
Link to comment
Share on other sites

Thanks @horst

This pointed me in the right direction, looking at some preg_replace examples within that module.  I used the online preg_replace tool here: https://www.phpliveregex.com/#tab-preg-replace to finally get what I was looking for: ?

$string = '<img alt="" src="/site/assets/files/10877/img_543.jpeg" />';
$pattern = '[\/site\/assets\/files\/(\d){1,20}\/]';
$replacement = '/site/assets/files/' . $page->id . '/';
preg_replace($pattern, $replacement, $string);

Output: 
<img alt="" src=/site/assets/files/<new_page_id>/img_543.jpeg" />

Thanks!

Edited by grimezy
updated code to include delimiters for preg_replace pattern.
  • Like 2
Link to comment
Share on other sites

This was the code I used to replace all items, just in case anyone needs the same in the future. ? 

$blog_pages = $pages->find("template=blog, include=all");

foreach($blog_pages as $p) {
  foreach($p->content_repeater as $item) {
    if($item->type == 'rm_text') {

      $item->of(false);

      $string = $item->body;
      $pattern = '[\/site\/assets\/files\/(\d){1,20}\/]';
      $replacement = '/site/assets/files/' . $p->id . '/';
      $new_string = preg_replace($pattern, $replacement, $string);

      $item->body = $new_string;

      $item->save();
      
    }
  }
}

Note: Don't forget to disable 'Fix broken images' under HTML options under your body content field, for the images not to be stripped from the output!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...