Search:

Categories



PHP String functions

Function Description
addcslashes()  Returns a string with backslashes in front of the specified characters
addslashes()  Returns a string with backslashes in front of predefined characters
bin2hex()  Converts a string of ASCII characters to hexadecimal values
chop()  Removes whitespace or other characters from the right end of a string
chr()  Returns a character from a specified ASCII value
chunk_split()  Splits a string into a series of smaller parts
convert_cyr_string()  Converts a string from one Cyrillic character-set to another
convert_uudecode()  Decodes a uuencoded string
convert_uuencode()  Encodes a string using the uuencode algorithm
count_chars()  Returns information about characters used in a string
crc32()  Calculates a 32-bit CRC for a string
crypt()  One-way string encryption (hashing)
echo()  Outputs one or more strings
explode()  Breaks a string into an array
fprintf()  Writes a formatted string to a specified output stream
get_html_translation_table()  Returns the translation table used by htmlspecialchars() and htmlentities()
hebrev()  Converts Hebrew text to visual text
hebrevc()  Converts Hebrew text to visual text and new lines (\n) into <br>
hex2bin()  Converts a string of hexadecimal values to ASCII characters
html_entity_decode()  Converts HTML entities to characters
htmlentities()  Converts characters to HTML entities
htmlspecialchars_decode()  Converts some predefined HTML entities to characters
htmlspecialchars()  Converts some predefined characters to HTML entities
implode()  Returns a string from the elements of an array
join()  Alias of implode()
lcfirst()  Converts the first character of a string to lowercase
levenshtein()  Returns the Levenshtein distance between two strings
localeconv()  Returns locale numeric and monetary formatting information
ltrim()  Removes whitespace or other characters from the left side of a string
md5()  Calculates the MD5 hash of a string
md5_file()  Calculates the MD5 hash of a file
metaphone()  Calculates the metaphone key of a string
money_format()  Returns a string formatted as a currency string
nl_langinfo()  Returns specific local information
nl2br()  Inserts HTML line breaks in front of each newline in a string
number_format()  Formats a number with grouped thousands
ord()  Returns the ASCII value of the first character of a string
parse_str()  Parses a query string into variables
print()  Outputs one or more strings
printf()  Outputs a formatted string
quoted_printable_decode()  Converts a quoted-printable string to an 8-bit string
quoted_printable_encode()  Converts an 8-bit string to a quoted printable string
quotemeta()  Quotes meta characters
rtrim()  Removes whitespace or other characters from the right side of a string
setlocale()  Sets locale information
sha1()  Calculates the SHA-1 hash of a string
sha1_file()  Calculates the SHA-1 hash of a file
similar_text()  Calculates the similarity between two strings
soundex()  Calculates the soundex key of a string
sprintf()  Writes a formatted string to a variable
sscanf()  Parses input from a string according to a format
str_getcsv()  Parses a CSV string into an array
str_ireplace()  Replaces some characters in a string (case-insensitive)
str_pad()  Pads a string to a new length
str_repeat()  Repeats a string a specified number of times
str_replace()  Replaces some characters in a string (case-sensitive)
str_rot13()  Performs the ROT13 encoding on a string
str_shuffle()  Randomly shuffles all characters in a string
str_split()  Splits a string into an array
str_word_count()  Count the number of words in a string
strcasecmp()  Compares two strings (case-insensitive)
strchr()  Finds the first occurrence of a string inside another string (alias of strstr())
strcmp()  Compares two strings (case-sensitive)
strcoll()  Compares two strings (locale based string comparison)
strcspn()  Returns the number of characters found in a string before any part of some specified characters are found
strip_tags()  Strips HTML and PHP tags from a string
stripcslashes()  Unquotes a string quoted with addcslashes()
stripslashes()  Unquotes a string quoted with addslashes()
stripos()  Returns the position of the first occurrence of a string inside another string (case-insensitive)
stristr()  Finds the first occurrence of a string inside another string (case-insensitive)
strlen()  Returns the length of a string
strnatcasecmp()  Compares two strings using a “natural order” algorithm (case-insensitive)
strnatcmp()  Compares two strings using a “natural order” algorithm (case-sensitive)
strncasecmp()  String comparison of the first n characters (case-insensitive)
strncmp()  String comparison of the first n characters (case-sensitive)
strpbrk()  Searches a string for any of a set of characters
strpos()  Returns the position of the first occurrence of a string inside another string (case-sensitive)
strrchr()  Finds the last occurrence of a string inside another string
strrev()  Reverses a string
strripos()  Finds the position of the last occurrence of a string inside another string (case-insensitive)
strrpos()  Finds the position of the last occurrence of a string inside another string (case-sensitive)
strspn()  Returns the number of characters found in a string that contains only characters from a specified charlist
strstr()  Finds the first occurrence of a string inside another string (case-sensitive)
strtok()  Splits a string into smaller strings
strtolower()  Converts a string to lowercase letters
strtoupper()  Converts a string to uppercase letters
strtr()  Translates certain characters in a string
substr()  Returns a part of a string
substr_compare()  Compares two strings from a specified start position (binary safe and optionally case-sensitive)
substr_count()  Counts the number of times a substring occurs in a string
substr_replace()  Replaces a part of a string with another string
trim()  Removes whitespace or other characters from both sides of a string
ucfirst()  Converts the first character of a string to uppercase
ucwords()  Converts the first character of each word in a string to uppercase
vfprintf()  Writes a formatted string to a specified output stream
vprintf()  Outputs a formatted string
vsprintf()  Writes a formatted string to a variable
wordwrap()  Wraps a string to a given number of characters
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

PHP Math functions

PHP Function Description
abs() Returns the absolute (positive) value of a number
acos() Returns the arc cosine of a number
acosh() Returns the inverse hyperbolic cosine of a number
asin() Returns the arc sine of a number
asinh() Returns the inverse hyperbolic sine of a number
atan() Returns the arc tangent of a number in radians
atan2() Returns the arc tangent of two variables x and y
atanh() Returns the inverse hyperbolic tangent of a number
base_convert() Converts a number from one number base to another
bindec() Converts a binary number to a decimal number
ceil() Rounds a number up to the nearest integer
cos() Returns the cosine of a number
cosh() Returns the hyperbolic cosine of a number
decbin() Converts a decimal number to a binary number
dechex() Converts a decimal number to a hexadecimal number
decoct() Converts a decimal number to an octal number
deg2rad() Converts a degree value to a radian value
exp() Calculates the exponent of e
expm1() Returns exp(x) – 1
floor() Rounds a number down to the nearest integer
fmod() Returns the remainder of x/y
getrandmax() Returns the largest possible value returned by rand()
hexdec() Converts a hexadecimal number to a decimal number
hypot() Calculates the hypotenuse of a right-angle triangle
is_finite() Checks whether a value is finite or not
is_infinite() Checks whether a value is infinite or not
is_nan() Checks whether a value is ‘not-a-number’
lcg_value() Returns a pseudo random number in a range between 0 and 1
log() Returns the natural logarithm of a number
log10() Returns the base-10 logarithm of a number
log1p() Returns log(1+number)
max() Returns the highest value in an array, or the highest value of several specified values
min() Returns the lowest value in an array, or the lowest value of several specified values
mt_getrandmax() Returns the largest possible value returned by mt_rand()
mt_rand() Generates a random integer using Mersenne Twister algorithm
mt_srand() Seeds the Mersenne Twister random number generator
octdec() Converts an octal number to a decimal number
pi() Returns the value of PI
pow() Returns x raised to the power of y
rad2deg() Converts a radian value to a degree value
rand() Generates a random integer
round() Rounds a floating-point number
sin() Returns the sine of a number
sinh() Returns the hyperbolic sine of a number
sqrt() Returns the square root of a number
srand() Seeds the random number generator
tan() Returns the tangent of a number
tanh() Returns the hyperbolic tangent of a number
INF INF The infinite PHP 4
M_E 2.7182818284590452354 Returns e PHP 4
M_EULER 0.57721566490153286061 Returns Euler constant PHP 4
M_LNPI 1.14472988584940017414 Returns the natural logarithm of PI: log_e(pi) PHP 5.2
M_LN2 0.69314718055994530942 Returns the natural logarithm of 2: log_e 2 PHP 4
M_LN10 2.30258509299404568402 Returns the natural logarithm of 10: log_e 10 PHP 4
M_LOG2E 1.4426950408889634074 Returns the base-2 logarithm of E: log_2 e PHP 4
M_LOG10E 0.43429448190325182765 Returns the base-10 logarithm of E: log_10 e PHP 4
M_PI 3.14159265358979323846 Returns Pi PHP 4
M_PI_2 1.57079632679489661923 Returns Pi/2 PHP 4
M_PI_4 0.78539816339744830962 Returns Pi/4 PHP 4
M_1_PI 0.31830988618379067154 Returns 1/Pi PHP 4
M_2_PI 0.63661977236758134308 Returns 2/Pi PHP 4
M_SQRTPI 1.77245385090551602729 Returns the square root of PI: sqrt(pi) PHP 5.2
M_2_SQRTPI 1.12837916709551257390 Returns 2/square root of PI: 2/sqrt(pi) PHP 4
M_SQRT1_2 0.70710678118654752440 Returns the square root of 1/2: 1/sqrt(2) PHP 4
M_SQRT2 1.41421356237309504880 Returns the square root of 2: sqrt(2) PHP 4
M_SQRT3 1.73205080756887729352 Returns the square root of 3: sqrt(3) PHP 5.2
NAN NAN Not A Number PHP 4
PHP_ROUND_HALF_UP 1 Round halves up PHP 5.3
PHP_ROUND_HALF_DOWN 2 Round halves down PHP 5.3
PHP_ROUND_HALF_EVEN 3 Round halves to even numbers PHP 5.3
PHP_ROUND_HALF_ODD 4 Round halves to odd numbers PHP 5.3
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

CSV to array

function csv_to_array($filename, $delimiter) {
    ini_set('auto_detect_line_endings', true);

    if(!file_exists($filename) || !is_readable($filename)) {
        return FALSE;
    }

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)   {
        while (($row = fgetcsv($handle, $delimiter)) !== FALSE)
        {
            if(!$header) {
                $header = $row;
            } else {
                $data[] = array_combine($header, $row);
            }
        }
        fclose($handle);
    }
    return $data;
}
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

List all active plugins

Lists all active plugins in wordpress, may be handy if you run a few sites

function wp_list_active_site_plugins() {
    $the_plugs = get_option('active_plugins'); 
    foreach($the_plugs as $key => $value) {
        $string = explode('/',$value); // Folder name will be displayed
        echo $string[0] ."\n";
    }
}

wp_list_active_site_plugins();
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

Convert Seconds to String

This function converts seconds to an easily readable string

<?php
function secondsToString($secs) 
{
	$r=0;
	if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
	$r.=$secs.' second';if($secs<>1){$r.='s';}
	return $r;
}

//1 day, 22 hours, 35 minutes, 10 seconds
echo secondsToString(167710);
//268 days, 26 minutes, 18 seconds
echo secondsToString(23156778);
//03 minutes, 55 seconds
echo secondsToString(235);


?>
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare

Get zodiac star sign for a date

 <?php
function zodiac($DOB){
    $DOB = date("m-d", strtotime($DOB));
    list($month,$day) = explode("-",$DOB);
    if(($month == 3 || $month == 4) && ($day > 22 || $day < 21)){
        $zodiac = "Aries";
    }
    elseif(($month == 4 || $month == 5) && ($day > 22 || $day < 22)){
        $zodiac = "Taurus";
    }
    elseif(($month == 5 || $month == 6) && ($day > 23 || $day < 22)){
        $zodiac = "Gemini";
    }
    elseif(($month == 6 || $month == 7) && ($day > 23 || $day < 23)){
        $zodiac = "Cancer";
    }
    elseif(($month == 7 || $month == 8) && ($day > 24 || $day < 22)){
        $zodiac = "Leo";
    }
    elseif(($month == 8 || $month == 9) && ($day > 23 || $day < 24)){
        $zodiac = "Virgo";
    }
    elseif(($month == 9 || $month == 10) && ($day > 25 || $day < 24)){
        $zodiac = "Libra";
    }
    elseif(($month == 10 || $month == 11) && ($day > 25 || $day < 23)){
        $zodiac = "Scorpio";
    }
    elseif(($month == 11 || $month == 12) && ($day > 24 || $day < 23)){
        $zodiac = "Sagittarius";
    }
    elseif(($month == 12 || $month == 1) && ($day > 24 || $day < 21)){
        $zodiac = "Cpricorn";
    }
    elseif(($month == 1 || $month == 2) && ($day > 22 || $day < 20)){
        $zodiac = "Aquarius";
    }
    elseif(($month == 2 || $month == 3) && ($day > 21 || $day < 21)){
        $zodiac = "Pisces";
    }

    return $zodiac;
}
//usage example
echo zodiac('1976-12-06'); //Valid strtotime date
echo "<br>";
echo zodiac('1986-04-04');
?> 
FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditDeliciousShare