Jump to content

[SOLVED] PHP "condense" Text


Vivid Lust

Recommended Posts

Could you be a little more descriptive?

 

How would you like to do this?  Cut off the end of the paragraph after a certain amount of words?

 

You could just explode the paragraph and only keep the first x amount of words...  but again, need more detail.

Thanks for the help, however im finding it hard to use substr.

 

Could some please post me the code, so that I can turn:

 

"With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post."

 

Into: "With Quick-Reply you can write"

 

There the code limits the paragraph to the first 5 words.

 

Thanks all.

* Not tested * - This SHOULD echo the first 4 words of the string.

 

$string = "With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post."

$short = "";
$pieces = explode(" ", $string);
for($i = 0; $i $short .= $pieces[$i] . " ";

echo $short;

This is assuming you want it to be cut into 10 words, roughly 50 characters:

 

<?php
$text = "With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.";

if (strlen($text) > 50) {
    $chopped = substr($text, 0, 50);
    echo $chopped . "...";
}else {
    echo $text;
}
?>

 

Simple as that.

 

Should do it.

 

 

EDIT:

This is an example using substr

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.