Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 12th, 2004, 12:40 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter12

I am having a problem understading the function TrimBody from the outputfunctions.php.Mainly I am having a problem understanding the reason behind the use of the variable $s_cnt. I even gave it a value of 999 and the output did not change at all. I tried an article with several lines, but no matter what the value of $s_cnt the output was of the trimbody function always seemed to be the same. Can somebody please explain me, why?

Thanks
Christian

__________________
Christian
Reply With Quote
  #2 (permalink)  
Old June 12th, 2004, 04:42 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As it states on page 392/393 of the book:

Quote:
quote:function trimBody($theText, $lmt=500, $s_chr="\n", $s_cnt=2) {

The function trimBody() will take a text input and return a shortened (trimmed) version for display on a page. If an article is very long, you might want to show only the first couple of sentences, or only the first paragraph. If the article is trimmed, you should end the trimmed text with ellipses ( . . . ).

The first parameter, $theText, is the text you want trimmed. The second parameter ($lmt) is the absolute longest text string you want returned, expressed in number of characters. The default value, if none is supplied, is 500 characters. The third parameter ($s_chr) is the "stop character." We will trim the document to this character. The default value is a newline (\n). The last parameter is $s_cnt, or "stop count." Given the stop character (in this case \n), we will trim after we reach the number designated by $s_cnt. In this case we default to 2, so we will trim the text after the second newline character.
What that is telling you is that if you pass in a period (.) as your stop character ($s_chr), then if $s_cnt = 2, it will trim your string to the second period, or 500 characters, whichever is smallest. When assigning values to the variables, you make those values the default. If you don't pass those values to the function, it will use those default values. In this case, if all you do is pass a string to the trimBody() function, it will trim to 500 characters, or to the 2nd newline (\n), whichever is smallest.

So, setting $s_cnt to 999 tells the function to keep going until it finds the 999th stop character. Of course, it will reach the length limit of 500 LONG before that!

Make sense?

BuzzLY
aka Michael K. Glass
Author, Beginning PHP, Apache, MySQL Web Development
Reply With Quote
  #3 (permalink)  
Old June 12th, 2004, 09:44 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Much clearer now. But you see I used the following lines:
This is a tes1
This is a tst2
This is a test3

and not matter what value of $s_cnt was It will always cut of after the first line and as you can is not even near to 500 characters. Any guess why is that?

Reply With Quote
  #4 (permalink)  
Old June 12th, 2004, 06:49 PM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm... let me test it on my server, and I'll get back to you.
------------
Ok, I have looked at the code and mucked around with it. It seems there is a problem with this line in outputfunctions.php. It's in the trimBody() function:

  if ($tmp = strpos($theText,$s_chr,$pos)) {

Change that line to:

  if ($tmp = strpos($theText,$s_chr,$pos+1)) {

The problem was that every time it looped through (which it does $s_cnt times), it found the first occurance. By adding one to $pos, we tell it to begin searching the next time through on the NEXT letter in the string.

Sorry for the confusion.


Michael K. Glass
Author, Beginning PHP, Apache, MySQL Web Development
Reply With Quote
  #5 (permalink)  
Old June 13th, 2004, 11:59 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Let me try it out and I will get back to you.

Thanks for all the help.
Christian

Reply With Quote
  #6 (permalink)  
Old June 15th, 2004, 09:46 AM
Friend of Wrox
 
Join Date: Jun 2004
Location: Fairfield, Iowa, USA.
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It still does not work properly. Buzzly did it work for you?

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter12 powertrace BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 25th, 2007 12:12 PM
Forbidden access chapter12 page 361 chokk BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 March 22nd, 2005 05:07 PM



All times are GMT -4. The time now is 03:17 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.