Jump to content

[SOLVED] Cutting the characters displayed (MySQL)


Recommended Posts

Hello There!

 

I am making a small blog site for myself and instead of downloading packages i want to make it myself.

 

I am wondering if there is a way to cut the number of words displayed.. For example,

 

In blogs you usually have about 20 or 30 words displayed and then a link that says "Read More"

 

Is it the query that cuts it to 30 words? Or is it a function?

 

Can anyone shead some light on this please!

 

Many Thanks,

 

Jake ;D

$Text = Split(" ", $Text);

$Num = 30; //Number of words

$Start = 1;

$Total = "";

 

foreach ($Text as $v)

{

$Total = $Total . "" . $v . " ";

$Start = $Start + 1;

 

if($Start == $Num)

{

break;

}

}

 

it could be off by one word, in which case, adjust start to make it right

You can do it in the query with SUBSTR, but this would do characters. Not sure if MySQL has a word limiter.

 

Or if you want you could do it like so:

 

function limit_words($text, $words){
$split = explode(" ", $text);
for($i = 0; $i < $words; $i++){
	$newText[] = $split[$i];
}

return implode(" ", $newText);
}

echo limit_words("Hello there this should be reduced to two words!", 2);

 

There is probably even a function that already does it for you. But there we are. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.