Tell me more ×
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It's 100% free, no registration required.

What is the best practice for manipulating strings with PHP (output mode) that might contain straight quote marks (single or double)? I'm not totally clear how EE stores and outputs these normally, and so I've run into PHP parse errors in certain situations.

Is the only reliable method to use {exp:xml_encode}? For example:

$excerpt = '{exp:xml_encode}{post_excerpt}{/exp:xml_encode}';

...and are those single quotes above around the EE code better than using double quotes?

EE's implementation of {exp:xml_encode} incorporates the PHP strip_tags() function, so this is not always ideal (e.g the excerpt above might have a phrase wrapped in <strong> tags. But at the same time, it seems there's no other way to store and manipulate the field content in PHP if it contains single or double quotes.

share|improve this question
1  
Don't use PHP in the templates. If you know enough PHP to use it in the templates take a couple hours and learn about building your own plugins. Check out ellislab.com/expressionengine/user-guide/development/… and pkg.io to get your first plugin bootstrapped. – th3mus1cman May 5 at 19:09

1 Answer

up vote 1 down vote accepted

Try using the php function addslashes:

<?php
$str = "Is your name O'reilly?";

// Outputs: Is your name O\'reilly?
echo addslashes($str);
?>

So for EE you could have something like:

(UPDATE 1: the following does not work:)

<p>This is the title and excerpt of this entry: "
    <?php 
        $str = "<strong>". addslashes({title}) . "</strong> : ";
        $str .= addslashes({post_excerpt}); // combine title and post_excerpt
        echo  stripslashes($str); // remove the slashes.
    ?>
"</p>

UPDATE 1 the following does work:

Following the suggestion of th3mus1cman above, this does work in a plugin without needing addslashes:

On your template:
<p>This is the excerpt of this entry: "

    {exp:test_php}{post_excerpt}{/exp:test_php}

"</p>

In your test_php plugin: 
public function __construct()
{
    $ee_str = ee()->TMPL->tagdata;
    $new_str = "<strong>". $ee_str . "</strong>";

    $this->return_data = $new_str;
}
share|improve this answer
But won't addslashes not be recognized as a function when it's inside the quotes? – James Muspratt May 5 at 19:38
edited code example – Romans-8---31-39 May 5 at 19:49
Well that illustrates the problem: strings have to be quoted in PHP, and your updated example will get processed by PHP as $str .= addslashes(Here's my "difficult" excerpt); That's going to throw a parse error, as will $str .= addslashes('Here's my "difficult" excerpt'); and $str .= addslashes("Here's my "difficult" excerpt"); In other words, with PHP set to Output (parsed after EE tags), either you must convert the quotes to HTML entities via {exp:xml_encode} or you run addslashes() via a plugin. I tried PHPStringFun but had issues. – James Muspratt May 5 at 20:10
Yep, sorry about the confusion above, I've updated it to include the simple plugin code I tested. Hopefully going the plugin route will be the answer. – Romans-8---31-39 May 5 at 21:34
Got it, thanks. The plugin route does make sense. Thanks! – James Muspratt May 5 at 23:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.