SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot TheMonk's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help understanding this function...

    I'm working through some tutorials and for some reason I can't seem to figure out this function. It basically takes a series of paragraphs and truncates the text so it can be used as a lead in paragraph on a home page. (like an online newspaper homepage etc...) Basically the text is either truncated at the limit, or at the second carriage return (\n).

    Here's the function:

    PHP Code:
    function trimBody($theText$lmt=500$s_chr="\n"$s_cnt=2) {
      
    $pos 0;
      
    $trimmed FALSE;
      for (
    $i 1$i <= $s_cnt$i++) {
        if (
    $tmp strpos($theText$s_chr$pos+1)) {
          
    $pos $tmp;
          
    $trimmed TRUE;
        } else {
          
    $pos strlen($theText) - 1;
          
    $trimmed FALSE;
          break;
        }
      }
      
    $theText substr($theText0$pos);

      if (
    strlen($theText) > $lmt) {
        
    $theText substr($theText0$lmt);
        
    $theText substr($theText0strrpos($theText,' '));
        
    $trimmed TRUE;
      }
      if (
    $trimmed$theText .= '...';
      return 
    $theText;

    Ok so everthing makes sense except the for the if statement in the loop. Here's what the tutorial says:

    "When you loop through the text $s_cnt times, if you find the character with the text on the last cycle through, then $trimmed will be true, and the text will be trimmed to that character.."

    Ok so looking at the loop it reads:
    PHP Code:
    for ($i 1$i <= $s_cnt$i++) {
        if (
    $tmp strpos($theText$s_chr$pos+1)) {
          
    $pos $tmp;
          
    $trimmed TRUE;
        } else {
          
    $pos strlen($theText) - 1;
          
    $trimmed FALSE;
          break;
        }
      } 
    What I'm not understanding is this how it's only going to check "on the last cycle through" the way I read it, the loop will run once, it will find the return on the first pass, get it's position, add one to it's position (I'm not sure why that happens either) then save the position of the return to $pos and set everything to true.

    Can someone explain to me how they figure it will only run on the "last cycle through". Basically it should be returning the second carriage return but the way I read it, it's gonna stop at the first it finds. And what's with the $pos+1 ?

    I know this should be easy but for some reason I'm stumped.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is not the sort of code you can learn anything from.
    Don't waste your time studying it.

  3. #3
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In all fairness that doesn't really help TheMonk.

    The info you need is in the PHP manual. $pos+1 is telling the script to ignore the first character of $theText (first time through the loop), or start at the next character of $theText (2nd time through the loop).

    If it finds $s_chr (e.g. \n) first time through the loop then it records the character position ($pos) then goes through the loop to find the next instance of $s_chr. If it does NOT find $s_chr within $theText then it sets $pos to the length of $theText and breaks out of the for loop.

    Therefore $theText = substr($theText, 0, $pos); returns the required info. The next section of code is checking whether the returned string is too long (> $lmt).
    Ian Anderson
    www.siteguru.co.uk

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wheres the tutorial at? could I have the address?

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    NO! I'm with stereofrog on this one
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I agree it's not good code, but saying you can't learn anything from it is misdirection.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    As a further to the NO!
    Here is a quick function I wrote to help:
    PHP Code:
    function substring($string$paragraphs=null$string_length=null) {
         
    $returnString '';
        if(
    $paragraphs) {
            
    $x explode("\n"$string);
            for(
    $i=0$i<=$paragraphs$i++) {
                
    $returnString .= $x[$i];
            }
        } else
        if(
    $string_length) {
            
    $x explode(" "$string);
            for(
    $i=0$i<=$string_length$i++) {
                
    $returnString .= $x[$i].' ';            
            }
        }
        
        return 
    $returnString;    

    USAGE:
    PHP Code:
    // return 4 paragraphs
    echo substring($text4);
    //
    // return 100 words
    echo substring($text''100); 
    $text is yout incoming text string....
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont get it...why isnt learning how this function works a waste of time?

    I think, if you're new to PHP theres a lot to be learned from this.....reading through a function and making sense of how it works has surely got to be the best training for learning to write your own?

    so its the not best php code ever, well theres something else to be learned from it there aswell!

    just my two cents worth!

  9. #9
    SitePoint Zealot TheMonk's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Folks,

    Thanks for the answers.

    elduderino; the tutorial is part of a book I'm working through so it's not available online unfortunately.

    I am curious to know why some folks don't think it's worth learning this code. Not that I'm defending the code by any means, what I am is curious to know what they think. Is the code just inefficient? Is it full or errors? Please elaborate so I can learn to see what the issues are.

    Siteguru thanks for the answer, I'm at work at the moment but I'll spend some time going over it later tonight.

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    It's not so much that its 'BAD' code it's just a long way of doing something that is quite simple. Learn from the code and understand what it does and then look at how it could be improved once you understand why it works.

    Learn from it but dont pick up bad habits!!!!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheMonk View Post
    I am curious to know why some folks don't think it's worth learning this code.
    Well, this is rather hard to explain. This function is not buggy in strict sense, but just ugly. It is inconsistent, verbose, uses unnecessary variables and operators etc.

    Programming is a design job and is about creating nice and handy things. It's not always possible to tell for certain whether a code is "nice" or not, the programmer must also trust his own aesthetic taste. Essentially, this internal feeling is what makes a good programmer.

  12. #12
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I reckon my function is pretty nifty that does the same thing
    PHP Code:
    // $s = content to trim, $l = desired length, $e = 0 or 1 - if 1 at end of string put '...'
    function nicetrim ($s,$l,$e) {

      
    $MAX_LENGTH $l;
      
    $str_to_count html_entity_decode($s);
      if (
    strlen($str_to_count) <= $MAX_LENGTH) {
       return 
    $s;
      }

      
    $s2 substr($str_to_count0$MAX_LENGTH);
      
    ## the below will add ... to the end
      
    if ($e == 1) {
      
    $s2 substr($str_to_count0$MAX_LENGTH 3);
      
    $s2 .= "...";
      }
      return 
    htmlentities($s2);

    mind you it doesn't take into account \n
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  13. #13
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Won't do exactly the same thing - your version may stop mid-word.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •