Dan Lee
Dan Lee

Reputation: 704

Rendering Quote Marks in PHP String

I have a PHP function that outputs quote marks like so when rendered in the html.

onload="this.rel='stylesheet'"

What I want is the following:

onload="this.rel='stylesheet'"

Here is the function causing the first example to happen - does anyone know how I can solve this?

public function get_css($opts, $index, $count)
    {
        $out = array();

                $version = filemtime($_SERVER['DOCUMENT_ROOT'].'/assets/css/app.min.css');
                $str = "this.rel='stylesheet'";
                $out[] = $this->_single_tag('link', array(
                    'rel'=>'preload',
                    'as'=>'style',
                    'type'=>'text/css',
                    'href'=>'/assets/css/app.min.'.$version.'.css',
                    'onload'=>$str,
                ));

        return implode("\n\t", $out)."\n";

    }

Here is the function for _single_tag

protected function _single_tag($tag=false, array $attrs)
{
    if ($tag===false) return;

    return PerchXMLTag::create($tag, 'single', $attrs);
}

Upvotes: 0

Views: 72

Answers (2)

nevada_scout
nevada_scout

Reputation: 983

The problem comes from the PerchXMLTag::create() method which does some HTML encoding on the values supplied to it.

Looking at the Perch documentation there doesn't seem to be a way to disable this, so my suggestion would be to replace the code within the get_css function with something that just outputs the raw HTML:

public function get_css($opts, $index, $count)
{
    $out = array();

    $version = filemtime($_SERVER['DOCUMENT_ROOT'].'/assets/css/app.min.css');
    $str = "this.rel='stylesheet'";
    $out[] = "<link rel='preload' as='style' type='text/css' href='/assets/css/app.min.{$version}.css' onload='{$str}' />";

    return implode("\n\t", $out)."\n";

}

Upvotes: 1

public function get_css($opts, $index, $count)
{
    $out = array();

            $version = filemtime($_SERVER['DOCUMENT_ROOT'].'/assets/css/app.min.css');
            $str = "this.rel=\"stylesheet\"";
            $out[] = $this->_single_tag('link', array(
                'rel'=>'preload',
                'as'=>'style',
                'type'=>'text/css',
                'href'=>'/assets/css/app.min.'.$version.'.css',
                'onload'=>$str,
            ));

    return implode("\n\t", $out)."\n";

}

Try with that.

Upvotes: 0

Related Questions