0

I’ve tags in following format spread all over my HTML enabled content entries:

<img alt="" src="{assets_2100:{filedir_12}HappyDog.jpg}" style="height:400px; width:100px">

I need to update all these in following format either via a PHP function before displaying at frontend or just update the MySQL once and for all:

<img alt="" src="/uploads/HappyDog.jpg" data-assetid="2100" data-filedir="12" data-filename="HappyDog.jpg" style="height:400px; width:100px”> 

The “/uploads/’ directory path is applicable for all images!

These tags were carried over when migrated content from EE

Edited: Okay, so far I tried the following as first step to remove the {filedir_X} from address:

    $parse_encoded = true;
    $pattern = ($parse_encoded)
        ? '/(?:{|&#123;)filedir_(\d+)(?:}|&#125;)/'
        : '/{filedir_(\d+)}/';
    if (preg_match_all($pattern, $htmlcontent, $matches, PREG_SET_ORDER))
    {
        foreach ($matches as $match){
            $htmlcontent = str_replace($match[0], '', $htmlcontent);
        }
    }

Then modified this to remove “{asset”, but ending up with “}” at the “src” attribute!

5
  • what format is this anyway ? Commented May 24, 2018 at 13:16
  • @MarkoPaju This format was applicable on Expression Engine, but at this point I just need to reserve all attribute for later JS manipulation before removing them from “src” attribute! Commented May 24, 2018 at 13:24
  • In my opinion it's better to understand the regular expressions, however a working example could help in getting to understand it. Not convinced it's the best approach but take a look on txt2re.com. Commented May 24, 2018 at 13:54
  • Maybe you could start with this 3v4l.org/2KMsU Commented May 24, 2018 at 13:55
  • Since this is because of a migration I think it's best to update the data in MySQL once and for all. This could even be done offline in a texteditor or whatever and then put back online. Commented May 24, 2018 at 13:56

1 Answer 1

1

Searching matches with preg_match_all to loop over results and to use str_replace is a waste of time. PHP has two functions for regex string replacements: preg_replace and preg_replace_callback (respectively for simple and complex replacements).

if ( $parseEncoded )
    $htmlContent = strtr($htmlContent, ['&#123;' => '{', '&#125;' => '}']);

$pattern = '~src=\K(["\']?){assets_(\d+):{filedir_(\d+)}([^}]+)}\1~i';
$replacement = '"/uploads/$4" data-assetid="$2" data-filedir="$3" data-filename="$4"';

$htmlContent = preg_replace($pattern, $replacement, $htmlContent);

Note that I changed the variable names: you have to choose between camelCase, snake_case and lowercase.

Sign up to request clarification or add additional context in comments.

1 Comment

Short, elegant and beautiful. Works like a charm. Thank you!

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.