Skip to content

Instantly share code, notes, and snippets.

@martinjlowm
Created September 15, 2011 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinjlowm/1220745 to your computer and use it in GitHub Desktop.
Save martinjlowm/1220745 to your computer and use it in GitHub Desktop.
Replace HTML paragraphs with breaks, assuming the content is in a <p>-wrapper that we want to keep.
<?php
$bodyText = $content['field_body']; // This is incorrect, the structure is different (use var_dump to figure it out)
$ptOne = substr($bodyText, 0, 3); // <p> - 0, 1, 2
$ptTwo = substr($bodyText, 3, -4); // text inside wrapper
$ptThree = substr($bodyText, -4); // </p> - 0, 1, 2, 3
$newcontent = preg_replace("/<p[^>]*?>/", "", $ptTwo);
$newcontent = str_replace("</p>", "<br /><br />", $newcontent);
$newcontent = $ptOne . $newcontent . $ptThree;
print render($newcontent);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment