Skip to content

Instantly share code, notes, and snippets.

@jonom
Last active December 10, 2015 20:38
Show Gist options
  • Save jonom/cd4916450ddc19e25117 to your computer and use it in GitHub Desktop.
Save jonom/cd4916450ddc19e25117 to your computer and use it in GitHub Desktop.
SilverStripe TextRaw2P extension. Converts raw text to html and replaces double line breaks with paragraphs.
<?php
/**
* TextRaw2P class.
* Apply this extension to the Text class to access $YourText.Raw2P in templates.
*
*/
class TextRaw2P extends DataExtension {
private static $casting = array(
'Raw2P' => 'HTMLText'
);
/**
* Convert raw text to html and replace double line breaks with paragraphs.
*
* Before:
* -------
* Hey this is a paragraph.
*
* And this is another one.
* With a manual line break.
* -------
*
* After:
* -------
* <p>Hey this is a paragraph.</p>
* <p>And this is another one.<br>
* With a manual line break.</p>
* -------
*
* @return string
*/
public function Raw2P() {
$xml = Convert::RAW2XML(trim($this->owner->value));
$xml = nl2br($xml);
return '<p>' . preg_replace('#(<br />[\r\n]+){2}#', "</p>\r\n<p>", $xml) . '</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment