mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Text->FirstParagraph?() now works for <div> containers in HTML, as you may not always have paragraph tags.
MINOR Added phpDoc comments above Text->FirstParagraph?() to note down the assumptions made with this method. (Merged from r64271) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.2@73365 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
4a11206b3f
commit
8c2037a677
@ -197,30 +197,44 @@ class Text extends DBField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Caution: Not XML/HTML-safe - does not respect closing tags.
|
* Take the first paragraph (the first <p> or <div> block in
|
||||||
|
* the content HTML that can be found) and returns it.
|
||||||
|
*
|
||||||
|
* CAUTION: Not XML/HTML-safe - does not respect closing tags.
|
||||||
|
*
|
||||||
|
* @param boolean $plain Return plain text (1) or (0) HTML text
|
||||||
|
* @return string The first paragraph of content
|
||||||
*/
|
*/
|
||||||
function FirstParagraph($plain = 1) {
|
function FirstParagraph($plain = 1) {
|
||||||
// get first sentence?
|
// get first sentence?
|
||||||
// this needs to be more robust
|
// this needs to be more robust
|
||||||
if($plain && $plain != 'html') {
|
if($plain && $plain != 'html') {
|
||||||
$data = Convert::xml2raw( $this->value, true );
|
$data = Convert::xml2raw( $this->value, true );
|
||||||
if( !$data ) return "";
|
if(!$data) return '';
|
||||||
|
|
||||||
// grab the first paragraph, or, failing that, the whole content
|
// grab the first paragraph, or, failing that, the whole content
|
||||||
if( strpos( $data, "\n\n" ) )
|
if(strpos( $data, "\n\n")) $data = substr( $data, 0, strpos( $data, "\n\n" ) );
|
||||||
$data = substr( $data, 0, strpos( $data, "\n\n" ) );
|
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if(strpos( $this->value, "</p>" ) === false) return $this->value;
|
|
||||||
|
|
||||||
$data = substr( $this->value, 0, strpos( $this->value, "</p>" ) + 4 );
|
// Find out what the container element for limit paragraphs should be
|
||||||
|
// (div or p is supported)
|
||||||
|
if(strpos($this->value, '</div>')) {
|
||||||
if(strlen($data) < 20 && strpos( $this->value, "</p>", strlen($data) )) $data = substr( $this->value, 0, strpos( $this->value, "</p>", strlen($data) ) + 4 );
|
$containerElement = '</div>';
|
||||||
|
} elseif(strpos($this->value, '</p>')) {
|
||||||
|
$containerElement = '</p>';
|
||||||
|
} else {
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
return $data;
|
$data = substr($this->value, 0, strpos($this->value, $containerElement) + strlen($containerElement));
|
||||||
|
|
||||||
|
if(strlen($data) < 20 && strpos($this->value, $containerElement, strlen($data))) {
|
||||||
|
$data = substr($this->value, 0, strpos($this->value, $containerElement, strlen($data)) + strlen($containerElement));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user