0

I have a string $content which looks like that

<h1>Or Any Other tags except img or nothing</h1>
...
<img src="{{media url=&quot;image_name.png&quot;}}" alt="image_test" />
...
<h1>Or Any Other tags except img or nothing</h1>

So as the minimal content of the string is

<img src="{{media url=&quot;dynamic_image_name.png&quot;}}" alt="dynamic_image_test_alt" />

What I want if to find a way to extract, alter and replace this specific line by the new one?

In the first place I made this:

protected function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

Using &quot; as from and to variable to get the filename. which is enough to generate what I want.

I would like to do something like that

$generatedContent = "<b>Hi test</b>";
$newContent = alterateContent($content,$generatedContent)

And the $newContent output needs to be:

<h1>Or Any Other tags except img or nothing</h1>
...
<b>Hi test</b>
...
<h1>Or Any Other tags except img or nothing</h1>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Claims
  • 245
  • 2
  • 15
  • How about using a [DOM parser](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) instead? It's always more robust and reliable than any string manipulation/parsing method. – Jeto Sep 02 '20 at 08:47
  • If you have a working solution using dom parser i'm fine with it. But the main goal isn't to print it into the dom but to alter a database field that will be used later. – Claims Sep 02 '20 at 09:00

2 Answers2

1

I would usually rarely recommend using regular expressions to parse HTML, but in your case, since your goal is to alter something in the database, parsing HTML and then saving it again might accidentally alter some other stuff that you'd want unchanged, such as the formatting.

So here's a simple solution using regex:

function alterateContent(string $html, string $imageFileName, string $replacement): string 
{
  $imageFileName = preg_quote($imageFileName, '/');

  return preg_replace(
    "/<img\h+src=\"{{media url=&quot;{$imageFileName}&quot;}}\".*?\/>/", 
    $replacement, 
    $html
  );
}

Usage:

$newContent = alterateContent($yourHtmlString, 'image_name.png', '<b>Hi test</b>');

Note: this assumes the src attribute is always the first attribute of the image.

Demo

Jeto
  • 14,596
  • 2
  • 32
  • 46
  • I assumed you needed to target a specific image file name. Otherwise, use @FlashThunder's solution instead, which is simpler. – Jeto Sep 02 '20 at 09:57
1

You can simply use preg_replace() for that, like this:

$newstring = preg_replace('~<img.*~','<b>Hi test</b>',$oldstring);

Without s modifier, it won't match new line character, so it should work just fine with inline replacement.

If you need to replace the img with exact src, you can do this like this:

$newstring = preg_replace('~<img src="'.$img_source.'".*~','<b>Hi test</b>',$oldstring);

If your source is only a filename without path, and in img tag it's with path, you can use this:

$newstring = preg_replace('~<img src=".*?'.$img_file.'".*~','<b>Hi test</b>',$oldstring);
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91