...

View Full Version : Please expand this abreviated if statement for me



jeddi
09-14-2009, 04:22 PM
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:


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:


$s = (strlen($line." ".$wrd) < $max )?" ":"<br>";
$line = ( $s===" " ) ? $line.$s.$wrd : $wrd;

I think they are compacted if statements :o

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


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.

SKDevelopment
09-14-2009, 04:31 PM
$s = (strlen($line." ".$wrd) < $max )?" ":"<br>";

is the same as


if(strlen($line." ".$wrd) < $max)
{
$s = " ";
} else {
$s = "<br>";
}


Also


$line = ( $s===" " ) ? $line.$s.$wrd : $wrd;

is the same as


if( $s===" " )
{
$line = $line.$s.$wrd;
} else {
$line = $wrd;
}


Edit: it is so called Ternary Operator (http://www.php.net/manual/en/language.operators.comparison.php) (you would need to scroll a little bit down at that page to see the definition).

jeddi
09-14-2009, 04:59 PM
Thanks for your quick reply:thumbsup:

So that function means this:


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 :o

Any ideas ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum