473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PHP Tokenize a String by its length

Hi Web Masters,

Just wondering wether you can help us to come up with some tokenize
script.

My problem is wanted to display a LONG content into a short para (by
giving minimum letter lenght)

I mean if I have a 2 page content - just I want to display only 4 lines
having 200 letters then follows the content with "....." at the end of
short pera.

Can anybody help in this regards,
Thanks in advance

Dec 31 '05 #1
10 4282
Message-ID: <11*********************@g49g2000cwa.googlegroups. com> from
Mavenos contained the following:
I mean if I have a 2 page content - just I want to display only 4 lines
having 200 letters then follows the content with "....." at the end of
short pera.

Can anybody help in this regards,
Thanks in advance


I wrote this function to do just that. It has the added feature that it
truncates on a space rather than the middle of the word.

$string is the long string to truncate
$length is the maximum truncated length
$url is the URL you should go to to see the whole paragraph

<?php
function truncate($string,$length,$url){
$string=trim($string);
if(strlen($string)>$length){
$string=substr($string,0,$length);
$n=0;
while(substr($string,-1)!=chr(32)){
$n++;
$string=substr($string,0,$length-$n);
}
$string=trim($string)."... <a href='$url'><em>more</em></a>";
}
return $string;
}

//example usage
echo truncate("the quick brown fox jumps over the lazy
dog",35,"anyurl");
?>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 31 '05 #2
Mavenos wrote:
Hi Web Masters,

Just wondering wether you can help us to come up with some tokenize
script.

My problem is wanted to display a LONG content into a short para (by
giving minimum letter lenght)

I mean if I have a 2 page content - just I want to display only 4 lines
having 200 letters then follows the content with "....." at the end of
short pera.

Can anybody help in this regards,
Thanks in advance


How about

$maxLetters = 300;
$shortContent = substr($longContent,0,$maxLetters).
(strlen($longContent) > $maxLetters ? "..." : "");
luph
Dec 31 '05 #3
Geoff Berrow wrote:
Message-ID: <11*********************@g49g2000cwa.googlegroups. com> from
Mavenos contained the following:
I mean if I have a 2 page content - just I want to display only 4 lines
having 200 letters then follows the content with "....." at the end of
short pera.

Can anybody help in this regards,
Thanks in advance


I wrote this function to do just that. It has the added feature that it
truncates on a space rather than the middle of the word.

$string is the long string to truncate
$length is the maximum truncated length
$url is the URL you should go to to see the whole paragraph

<?php
function truncate($string,$length,$url){
$string=trim($string);
if(strlen($string)>$length){
$string=substr($string,0,$length);
$n=0;
while(substr($string,-1)!=chr(32)){
$n++;
$string=substr($string,0,$length-$n);
}
$string=trim($string)."... <a href='$url'><em>more</em></a>";
}
return $string;
}

//example usage
echo truncate("the quick brown fox jumps over the lazy
dog",35,"anyurl");
?>


Shorter :)

function trancate($string,$length) {
$string = substr(trim($string),0,$length);
$string = substr($string,0,strrpos(trim($string)," "));
return "$string...";
}
Dec 31 '05 #4
Message-ID: <HWstf.630$i%4.305@trndny08> from Lüpher Cypher contained
the following:
Shorter :)

function trancate($string,$length) {
$string = substr(trim($string),0,$length);
$string = substr($string,0,strrpos(trim($string)," "));
return "$string...";
}

ahhhh, strrpos() Not used that one before.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 31 '05 #5
Message-ID: <HWstf.630$i%4.305@trndny08> from Lüpher Cypher contained
the following:
Shorter :)

function trancate($string,$length) {
$string = substr(trim($string),0,$length);
$string = substr($string,0,strrpos(trim($string)," "));
return "$string...";
}


You don't need the last trim()

$string = substr($string,0,strrpos($string," "));

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 31 '05 #6
Geoff Berrow wrote:
Message-ID: <HWstf.630$i%4.305@trndny08> from Lüpher Cypher contained
the following:
Shorter :)

function trancate($string,$length) {
$string = substr(trim($string),0,$length);
$string = substr($string,0,strrpos(trim($string)," "));
return "$string...";
}


You don't need the last trim()

$string = substr($string,0,strrpos($string," "));


Actually, it's needed :)
Suppose $string = "aaa bbb ccc" and $length = 9, then:
first substr leaves "aaa bbb "
if we don't have the second trim, strrpos returns 8 and second substr
returns "aaa bbb " so, "aaa bbb ..." will be returned instead of "aaa
bbb..." :)
luph
Dec 31 '05 #7
Message-ID: <sSttf.3225$gq4.1485@trndny04> from Lüpher Cypher contained
the following:
Actually, it's needed :)
Suppose $string = "aaa bbb ccc" and $length = 9, then:
first substr leaves "aaa bbb "
if we don't have the second trim, strrpos returns 8 and second substr
returns "aaa bbb " so, "aaa bbb ..." will be returned instead of "aaa
bbb..." :)


Think again.

strrpos returns 7

;-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 31 '05 #8
Geoff Berrow wrote:
Message-ID: <sSttf.3225$gq4.1485@trndny04> from Lüpher Cypher contained
the following:
Actually, it's needed :)
Suppose $string = "aaa bbb ccc" and $length = 9, then:
first substr leaves "aaa bbb "
if we don't have the second trim, strrpos returns 8 and second substr
returns "aaa bbb " so, "aaa bbb ..." will be returned instead of "aaa
bbb..." :)


Think again.

strrpos returns 7

;-)


Actually:

$str = "aaa bbb ccc";
$str = substr($str,0,9);
echo strrpos($str," ");
Outputs 8 :) There is a mistake, though:

instead of
$str = substr($str,0,strrpos(trim($str)," "));
it should be
$str1 = trim(substr($str,0,strrpos($str," ")));

Otherwise if it's trimmed first, it'll become "aaa bbb" and strrpos will
return 3, resulting in "aaa" :)
luph
Dec 31 '05 #9
Message-ID: <ZFwtf.636$i%4.91@trndny08> from Lüpher Cypher contained the
following:
Actually:

$str = "aaa bbb ccc";
$str = substr($str,0,9);
echo strrpos($str," ");
ah yes, missed the double space...

Outputs 8 :) There is a mistake, though:


I knew something was wrong. :-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Dec 31 '05 #10
I usually use a regular expression so that I'm not breaking in the
middle of a word:

preg_match('/^.{0,400}\b/s', $text, $m);
$text_shorten = $m[0];

Jan 1 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the...
9
by: Lans | last post by:
I have a string that I need to tokenize but I need to use a string token see example i am trying the following but strtok only uses characters as delimiters and I need to seperate bu a certain...
4
by: Kelvin | last post by:
hi: in C, we can use strtok() to tokenize a char* but i can't find any similar member function of string that can tokenize a string so how so i tokenize a string in C++? do it the C way? ...
2
by: James | last post by:
Hi, I am looking for a stringtokenizer class/method in C#, but can't find one. The similar classes in Java and C++ are StringTokenizer and CStringT::tokenize respectively. I need to keep a...
20
by: bubunia2000 | last post by:
Hi all, I heard that strtok is not thread safe. So I want to write a sample program which will tokenize string without using strtok. Can I get a sample source code for the same. For exp:...
1
by: Tim | last post by:
I ran into a problem with a script i was playing with to check code indents and need some direction. It seems to depend on if tabsize is set to 4 in editor and spaces and tabs indents are mixed on...
2
by: askalottaqs | last post by:
there's in maya's scripting language mel, called tokenize, you simply tokenize("string i want to tokenize"," ",bufferArray) which will fill the fufferArray wih the first string tokenized accorfing...
3
by: WP | last post by:
Hello! I need some help with my program...it's supposed to read infix expressions line by line from stdin and each expression should be divided into operands and operators and added to a vector of...
6
m6s
by: m6s | last post by:
1. After hours of researching, I used these snippets : void Object::TokenizeLines(const string& str, vector<string>& tokens, const string& delimiters) // Skip delimiters at beginning....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.