Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

First word of $content bold...


Go to End


9 Posts   3734 Views

Avatar
zim

Community Member, 135 Posts

28 October 2009 at 11:52pm

This is a long shot - but does anyone have a snippet of how to make the firast word of a returned $content bold.

Hope this makes sense.

Avatar
Juanitou

Community Member, 323 Posts

29 October 2009 at 11:20pm

Edited: 29/10/2009 11:22pm

It depends on the HTML content of your $Content field, but you should achieve it quite easily with a combination of CSS pseudo-elements :first-child and :first-letter.

Hope it helps,
Juan

EDIT: Sorry, this approach is useless, I read too fast… I thought you were talking about the first letter, not the first word.

Avatar
zim

Community Member, 135 Posts

29 October 2009 at 11:31pm

It is a shame there is no :first-word. Thanks for response though. I will remember this if I need to highlight first letter :)

Avatar
Mo

Community Member, 541 Posts

3 November 2009 at 12:37pm

Its a bit over the top, but you could use javascript/jquery. I am pulling this out of ass a bit, but I imiagine it would be something like:

// Assuming your content container div has  a class of Layout and
// your first element within that is a p tag. This returns the first para
// as an array split by a space character.
textString = $('div.Layout p:first').text().split(' ');

// Now get the first word from that array and add strong tags
firstWord = '<strong>' + textString.slice(0,1) + '</strong>';

// Create variable for reassembled para
reassemble = '';

// Loop through each item in the array, and add you bold word in place of the first item. 
for (i = 0; i < textString.length; i++) {
    if(i == 0)
        reassemble += firstWord + ' ';
    else
        reassemble += textString + ' ';
}

$('div.Layout p:first').text(reassemble);

I am sure there is a more efficient way of doing this (probably involving a regex), but its far to late for that :).

Avatar
zim

Community Member, 135 Posts

4 November 2009 at 3:10am

Thanks Mo. i will try this and let you know :)

Avatar
Graphicator

Community Member, 62 Posts

15 September 2011 at 12:00pm

Edited: 15/09/2011 12:00pm

I found out a regex function that will work for Pages. Thanks to UC @ www.silverstripe.org/archive/show/4383

New Field on Class "Page" in page.php

Get the first word

	function NewField_FirstWord(){
			list($Field_Split) = explode(' ', $this->NewField); //Found on the PHP Manual Website
			return $Field_Split;
	}

Get everything but the first word
	function NewField_AllButFirstWord(){
		list($Field_Split) = explode(' ', $this->NewField);  
		return preg_replace("/$Field_Split/", '', $this->NewField, 1); //Preg includes a count at the end, for PHP 5.0+
	}

Your NewField can be Content, Title, or whatever new field.

Page.ss

just call

<span>$NewField_FirstWord</span>$NewField_AllButFirstWord

Avatar
Devlin

Community Member, 344 Posts

15 September 2011 at 7:38pm

If $Content is a HTMLText, you should consider strip_tags before.

function Content() {
	$content = $this->Content;
	$content_plain = trim(strip_tags($content));
	$content_1st = current(explode(" ",$content_plain));
	$content = preg_replace("/$content_1st/", "<strong>$content_1st</strong>", $content, 1);
	return $content;
}

Avatar
SpiritLevel

Community Member, 5 Posts

23 October 2015 at 10:49am

Edited: 23/10/2015 11:05am

Here is another way to get first word, second word, etc:

 
public function MenuTitleFirstWord() {
	if ( str_word_count($this->MenuTitle) == 0 ) {
	    return null;
	} else {
	    return str_word_count($this->MenuTitle,1)[0]; 
	}
}

public function MenuTitleSecondWord() {
	if ( str_word_count($this->MenuTitle) <= 1 ) {
	    return null;
	} else {
	    return str_word_count($this->MenuTitle,1)[1]; 
	}
}

LimitWordCount for StringField may also be useful: http://api.silverstripe.org/3.2/source-class-StringField.html#_LimitWordCount

Go to Top