Regex replace double quotes inside double quotes

Hi folks. Can you help me please with adding backslash to double quotes inside double quotes string?

<iframe width="200" height="113" src="null" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen title=""Close The Doors!" (When Idiots Play Games #255)">

Have a look at the title tag!

Thanks in advance!

Are you putting the title in, or are you starting with that whole thing as a string?

Why? This appears to be a snippet of html. html does use backslashes. Instead it has it own method of converting certain punctuation into what it calls html entities. So double quotes becomes &quot ;. https://www.php.net/manual/en/function.htmlentities.php

Trying to do this sort of thing with regex is a losing proposition. Basically pretty much any user content needs to be html encoded to avoid breaking your actual html. Template languages such as Twig help automate this.

For plain php, do something like:

<?php
    // Original content
    $title = '"Close The Doors!" (When Idiots Play Games #255)';
    // Encoded content
    $titleEncoded = htmlspecialchars($title, ENT_COMPAT);
    // echo $titleEncoded . "\n";
?>
<iframe width="200" height="113" src="null" 
  frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
  allowfullscreen title="<?= $titleEncoded ?>"
>

And yes it is a pain to encode everything but it’s just part of generating html using php.

1 Like

This is why i asked if his string is JUST the title, or the whole HTML. Because htmlspecialchars isnt going to work if the whole thing is in his string, and you WILL need regex (or a lot of exploding/imploding).