Search code examples
phpechodouble-quotesapostrophe

PHP - Apostrophe and Double quotes on echo


echo '<meta property="article:published_time" content="<?php echo htmlentities($datePublished, \ENT_QUOTES, "UTF-8", false); ?>">';

result: <meta property="article:published_time" content="<?php echo htmlentities($datePublished, \ENT_QUOTES, "UTF-8", false); ?>"> And i get "> printed above the navbar

I know that it's because of the double quotes that starts on content=", but i need to put the UTF-8 on the code line.

I cant put "" and i can't put '', so what i do? There's a way to echo this?


Solution

  • You could make use of backslashes (\) to escape your quotes, however, I would recommend separating this out into three 'conjoined' echo statements (separated with .); one for the start of the HTML <meta>, one for the htmlentities(), and one for the end of the <meta>:

    echo '<meta property="article:published_time" content="' .
    htmlentities($datePublished, \ENT_QUOTES, "UTF-8", false) . 
    '">';
    

    Or on one line:

    echo '<meta property="article:published_time" content="' . htmlentities($datePublished, \ENT_QUOTES, "UTF-8", false) . '">';
    

    This will output:

    <meta property="article:published_time" content="SOME_DATE">