0

Im printing the node body in my tpl file with this:

 <?php print render(field_view_field("node", $node, "body", array(
  "label"=>"hidden",
  "type" => "text_summary_or_trimmed",
  "settings"=>array("trim_length" => 150),
))); ?>

The " and ' characters in the node's content are breaking the page as im putting this in a mailto link. How can I make sure these characters are printed as non html entities eg &#34; instead of ".

1 Answer 1

2

You could wrap it in PHP's htmlspecialchars and strip_tags functions:

$body_trimmed = render(field_view_field("node", $node, "body", array(
  "label"=>"hidden",
  "type" => "text_summary_or_trimmed",
  "settings"=>array("trim_length" => 150),
))); 
print htmlspecialchars(strip_tags($body_trimmed));

This would give you plain text with special characters encoded.

Alternately, if all you want to do is escape the quote characters and leave the divs in place, you could do a search/replace using str_replace for each, adding the backslash to escape them:

$body_trimmed = render(field_view_field("node", $node, "body", array(
  "label"=>"hidden",
  "type" => "text_summary_or_trimmed",
  "settings"=>array("trim_length" => 150),
))); 
$body_trimmed = str_replace("'","\'",$body_trimmed);
$body_trimmed = str_replace('"','\"',$body_trimmed);
print $body_trimmed;
12
  • That outputs all the divs as plain text.
    – Evanss
    Commented Oct 20, 2014 at 15:10
  • How do you want the divs to render if you are using this in a mailto link? Can you give an example of how you are using it?
    – Laryn
    Commented Oct 20, 2014 at 15:20
  • As its going to be in the body of an email I want plain text. I need to escape the ' and " characters as they break the link.
    – Evanss
    Commented Oct 20, 2014 at 15:34
  • See if the edits above help in your situation.
    – Laryn
    Commented Oct 20, 2014 at 15:52
  • Great's is converted to Great\'s. So the ' character is still breaking the link.
    – Evanss
    Commented Oct 20, 2014 at 16:39

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.