1

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.

1

1 Answer 1

1

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;
}
5
  • But won't addslashes not be recognized as a function when it's inside the quotes? Commented May 5, 2013 at 19:38
  • edited code example Commented May 5, 2013 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. Commented May 5, 2013 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. Commented May 5, 2013 at 21:34
  • Got it, thanks. The plugin route does make sense. Thanks! Commented May 5, 2013 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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