Thursday, June 3, 2010
Accessing Substrings with php
You want to extract part of a string, starting at a particular place in the string. For example,
you want the first eight characters of a username entered into a form.
Solution
Use substr( ) to select your substrings:
$substring = substr($string,$start,$length);
$username = substr($_REQUEST['username'],0,8);
Discussion
If $start and $length are positive, substr( ) returns $length characters in the string,
starting at $start. The first character in the string is at position 0:
print substr('watch out for that tree',6,5);
out f
If you leave out $length, substr( ) returns the string from $start to the end of the
original string:
print substr('watch out for that tree',17);
t tree
If $start plus $length goes past the end of the string, substr( ) returns all of the string
from $start forward:
print substr('watch out for that tree',20,5);
ree
If $start is negative, substr( ) counts back from the end of the string to determine where
your substring starts:
print substr('watch out for that tree',-6);
print substr('watch out for that tree',-17,5);
t tree
out f
If $length is negative, substr( ) counts back from the end of the string to determine
where your substring ends:
print substr('watch out for that tree',15,-2);
print substr('watch out for that tree',-4,-1);
hat tr
tre
--
Thanks & regards
Saxan Rappai.
moderator: http://makemytaste.com
Friday, May 28, 2010
Truncate a text in php. avoid splitting word with substr
There are certain situations when we need to truncate a text.. for example if we want to show only some parts of a text.. like what we see in google search.. a small description is shown..
to face such situations i have kept a function and named it as truncate.. and have included the code in my helper file.. so that i have access to this function from anywhere within the project..
the code is
<?php
/*
$string: is the text to truncate
$start : is the starting count of the text from which the text is to be truncated.. normaly it will be zero. taht is text is from the begining
$length : refers to the count of characters to return back. the function takes it as a apporximate value. because function searches for the nearby 'space' in the $roundof characters limit
so $roundof : is the count of characters for the near by space,,..
$append is the characters to be appended after the truncated string for example : " like this..." (three dots)
*/
function truncate($string, $start = 0,$length = 250 ,$append="...",$roundof=5){
if (strlen($string) < $length){
return $string;
}else{
$around=$length-$roundof;
$whitespaceposition = strpos($string," ",$around);
$truncated = substr($string, 0, $whitespaceposition);
$truncated.=$append;
return $truncated;
}
return TRUE;
}
// example
$text ="hai how are you friend";
// want to truncate the text in 5characters
print truncate($text,0,5);
/*
output
hai h...
*/
?>
--
Thanks & regards
Saxan Rappai.
moderator: http://makemytaste.com
to face such situations i have kept a function and named it as truncate.. and have included the code in my helper file.. so that i have access to this function from anywhere within the project..
the code is
<?php
/*
$string: is the text to truncate
$start : is the starting count of the text from which the text is to be truncated.. normaly it will be zero. taht is text is from the begining
$length : refers to the count of characters to return back. the function takes it as a apporximate value. because function searches for the nearby 'space' in the $roundof characters limit
so $roundof : is the count of characters for the near by space,,..
$append is the characters to be appended after the truncated string for example : " like this..." (three dots)
*/
function truncate($string, $start = 0,$length = 250 ,$append="...",$roundof=5){
if (strlen($string) < $length){
return $string;
}else{
$around=$length-$roundof;
$whitespaceposition = strpos($string," ",$around);
$truncated = substr($string, 0, $whitespaceposition);
$truncated.=$append;
return $truncated;
}
return TRUE;
}
// example
$text ="hai how are you friend";
// want to truncate the text in 5characters
print truncate($text,0,5);
/*
output
hai h...
*/
?>
--
Thanks & regards
Saxan Rappai.
moderator: http://makemytaste.com
Sunday, May 23, 2010
current date and time in php from apache server
Labels:
apache time in php,
how to get php date and time for mysql,
mysql datetime date and time from php,
mysql format time in php,
php server time to insert to mysql,
server time in php,
time in php
Posted by
saxan
at
1:16 AM
there is certain situation for logging the current time in mysql.. in such situation the mysql 'now()' function is not used by me.
since the mysql server time may be different than apache server .. some application may be using remote database access rather than localhost. in such situations too i use to go for a a function in php.
try this function such that the function is declared above the place where you are calling the function.
declare this function and just call this function. you will get the current apache server time.
function get_date(){
$timestamp=strftime("%Y-%m-%d %H:%M:%S %Y");
$curr=strftime("%Y-%m-%d %H:%M:%S", strtotime($timestamp));
return $curr;
}
?>
call this function such that
$currenttime=get_date();
print $currenttime;
?>
the output of this function will be the current date and time in a format for mysql " yyyy-mm-dd 00:00:00 " . you can simply enter this data to mysql datetime field.
since the mysql server time may be different than apache server .. some application may be using remote database access rather than localhost. in such situations too i use to go for a a function in php.
try this function such that the function is declared above the place where you are calling the function.
declare this function and just call this function. you will get the current apache server time.
$timestamp=strftime("%Y-%m-%d %H:%M:%S %Y");
$curr=strftime("%Y-%m-%d %H:%M:%S", strtotime($timestamp));
return $curr;
}
?>
call this function such that
$currenttime=get_date();
print $currenttime;
?>
the output of this function will be the current date and time in a format for mysql " yyyy-mm-dd 00:00:00 " . you can simply enter this data to mysql datetime field.
Subscribe to:
Posts (Atom)