473,545 Members | 289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4293
Message-ID: <11************ *********@g49g2 000cwa.googlegr oups.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($strin g,$length,$url) {
$string=trim($s tring);
if(strlen($stri ng)>$length){
$string=substr( $string,0,$leng th);
$n=0;
while(substr($s tring,-1)!=chr(32)){
$n++;
$string=substr( $string,0,$leng th-$n);
}
$string=trim($s tring)."... <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($longCon tent,0,$maxLett ers).
(strlen($longCo ntent) > $maxLetters ? "..." : "");
luph
Dec 31 '05 #3
Geoff Berrow wrote:
Message-ID: <11************ *********@g49g2 000cwa.googlegr oups.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($strin g,$length,$url) {
$string=trim($s tring);
if(strlen($stri ng)>$length){
$string=substr( $string,0,$leng th);
$n=0;
while(substr($s tring,-1)!=chr(32)){
$n++;
$string=substr( $string,0,$leng th-$n);
}
$string=trim($s tring)."... <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($strin g,$length) {
$string = substr(trim($st ring),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($strin g,$length) {
$string = substr(trim($st ring),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($strin g,$length) {
$string = substr(trim($st ring),0,$length );
$string = substr($string, 0,strrpos(trim( $string)," "));
return "$string... ";
}


You don't need the last trim()

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

--
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($strin g,$length) {
$string = substr(trim($st ring),0,$length );
$string = substr($string, 0,strrpos(trim( $string)," "));
return "$string... ";
}


You don't need the last trim()

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


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,s trrpos(trim($st r)," "));
it should be
$str1 = trim(substr($st r,0,strrpos($st r," ")));

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

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

Similar topics

16
2280
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 embedded comments I used the tokenize module. To my surprise the analysed output is different from the input (the last tuple element should exactly...
9
18414
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 word char *mystring "Jane and Peter and Tom and Cindy" char *delim = " and "; char *token; token = strtok(mystring, delim);
4
3115
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? thanks -- { Kelvin@!!! }
2
31370
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 current position within the string and change the delimiters dynamically when going throught the string. Does anyone know a stringtokenizer in c#? ...
20
17184
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: 0.0.0.0--->I want to tokenize the string using delimiter as as dot. Regards
1
2264
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 consecutive lines. Works fine when editors tabsize was 8 regardless if indents are mixed. Below are how the 3 test files are laid out, the...
2
2514
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 to the second string, isnt there anything as staight forward as this in c++?
3
2094
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 strings. So if we read one line that holds "1+2" the vector should afterwards hold the strings "1", "+" and "2". Valid operators are +, -, * and /...
6
4226
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. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters,...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.