Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Please expand this abreviated if statement for me

    Hello,

    I am trying to figure out how this functiom works so that I can
    get it to do what I want.

    The function is this:

    PHP Code:
    function wrap_it$str $max ){
    $str explode(" "$str );    
    $out "";
    $line "";
    foreach(
    $str as $wrd){
       
    $s = (strlen($line." ".$wrd) < $max )?" ":"<br>";
       
    $line = ( $s===" " ) ? $line.$s.$wrd $wrd;
       
    $out .= $s.$wrd;
       }
    return  
    $out;

    I don't understand these two lines:

    PHP Code:
    $s = (strlen($line." ".$wrd) < $max )?" ":"<br>";
    $line = ( $s===" " ) ? $line.$s.$wrd $wrd
    I think they are compacted if statements

    Can someone show me what they look like in this
    format:

    PHP Code:
    if( something) {
      do 
    something
     
    }
    else {
      do 
    something
     

    then I might be able to follow whats going on
    and fix my problem

    Thanks for any help.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    PHP Code:
    $s = (strlen($line." ".$wrd) < $max )?" ":"<br>"
    is the same as
    PHP Code:
    if(strlen($line." ".$wrd) < $max)
    {
     
    $s " ";
    } else {
     
    $s "<br>";

    Also
    PHP Code:
    $line = ( $s===" " ) ? $line.$s.$wrd $wrd
    is the same as
    PHP Code:
    if( $s===" " )
    {
     
    $line $line.$s.$wrd;
    } else {
     
    $line $wrd;

    Edit: it is so called Ternary Operator (you would need to scroll a little bit down at that page to see the definition).
    Last edited by SKDevelopment; 09-14-2009 at 03:37 PM.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for your quick reply

    So that function means this:

    PHP Code:
    function wrap_it($str,$max){
    $str explode(" "$str );    
    $out "";
    $line "";
    foreach(
    $str as $wrd){
       if(
    strlen($line." ".$wrd) < $max){
          
    $s " ";
                } 
         else {
                 
    $s "<br>";
                } 

        if( 
    $s===" " ) {
           
    $line $line.$s.$wrd;
           } 
            else {
           
    $line $wrd;
        }  
      
       
    $out .= $s.$wrd;
       }  
    // end foreach
    return  $out;

    Seems to be doing the same as wordwrap() does.

    My problem is that I want to wordwrap a file to
    70 characters but if there is a hyperlink, then I don't want to
    wrap it

    i.e. - if it sees this, it shoul skip the whole thing.

    <a href=\"http://www.my-long-name-site.com/something/login.php\">Login to this site</a>
    I suppose, I need to look for "<a href" and </a> using a preg_replace ?

    This is a bit out of my knowledge

    Any ideas ?


  •  

    Posting Permissions

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