Wednesday, February 16, 2011

Deal(trimming) with white blank-PHP

Assalamu'alaikum and selamat sejahtera

So,for this post,we will learn on how to deal with white blank on the input.Quite confusing right?okey,here is an example of event,sometime we input our name to the system like this " alexander " .So,from this quotation marks,we can see that there is an extra space at before and after the text.Besides,the PHP will count this extra space as part of the name,and of course you do not want your name got some extra space....ugh!its ugly lol,hahaha.here is the proof,,but before that,strlen() is use to count the length of the string....try this code:

<?PHP
$name_space=" alexander ";
$input_Count = strlen($name_space);
print $input_Count;
?>

it will print,11 ryte?but "alexander" got 9 letter ryte?.So,to settle this problem we just trim this name.trim()-it will tell the PHP,not to count the white blank as the letter.Here is the code:


<html>
<head>
<title>Lessons for Trimming space</title>


<?PHP

$name_space = trim(" alexander ");
$input_Count = strlen($name_space);
print $input_Count;


?>

</head>

<body>


</body>
</html>

  info
ltrim( )  -tell the PHP not to count space at beginning of a string
rtrim( )  -tell the PHP not to count space at end of a string

N/NOTE:hoooraay!!my name got no white blank anymore!!hahaha

No comments:

Post a Comment