1. str_replace (PHP 4, PHP 5)

str_replace — Replace all occurrences of the search string with the replacement string

Description
mixed str_replace ( mixed $search, mixed $replace, mixed $subject [, int &$count] )
This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.
// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");


2. strcmp (PHP 4, PHP 5)
strcmp — Binary safe string comparison
Description
int strcmp ( string $str1, string $str2 )
Note that this comparison is case sensitive.



3. str_shuffle (PHP 4 >= 4.3.0, PHP 5)

str_shuffle — Randomly shuffles a string
Description
string str_shuffle ( string $str )
str_shuffle() shuffles a string. One permutation of all possible is created.

$str = ‘abcdef’;
$shuffled = str_shuffle($str);
// This will echo something like: bfdaec
echo $shuffled;


4. str_split (PHP 5)

str_split — Convert a string to an array
Description
array str_split ( string $string [, int $split_length] )
Converts a string to an array.
If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.
FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.

$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);


5. strcasecmp (PHP 4, PHP 5)
strcasecmp — Binary safe case-insensitive string comparison
Description
int strcasecmp ( string $str1, string $str2 )
Binary safe case-insensitive string comparison.
$var1 = "Hello";
$var2 = "hello";
if (strcasecmp($var1, $var2) == 0) {
echo ‘$var1 is equal to $var2 in a case-insensitive string comparison’;
}


6. strpos (PHP 4, PHP 5)
strpos — Find position of first occurrence of a string
Description
int strpos ( string $haystack, mixed $needle [, int $offset] )
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos() before PHP 5, this function can take a full string as the needle parameter and the entire string will be used.
$mystring = ‘abc’;
$findme = ‘a’;
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of ‘a’ was the 0th (first) character.
if ($pos === false) {
echo "The string ‘$findme’ was not found in the string ‘$mystring’";
} else {
echo "The string ‘$findme’ was found in the string ‘$mystring’";
echo " and exists at position $pos";
}
// We can search for the character, ignoring anything before the offset
$newstring = ‘abcdef abcdef’;
$pos = strpos($newstring, ‘a’, 1); // $pos = 7, not 0


7. strrchr (PHP 4, PHP 5)
strrchr — Find the last occurrence of a character in a string
Description
string strrchr ( string $haystack, string $needle )
This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack.
// get last directory in $PATH
$dir = substr(strrchr($PATH, ":"), 1);
// get everything after last newline
$text = "Line 1nLine 2nLine 3";
$last = substr(strrchr($text, 10), 1 );


8. strrev (PHP 4, PHP 5)
strrev — Reverse a string
Description
string strrev ( string $string )
Returns string, reversed.
echo strrev("Hello world!"); // outputs "!dlrow olleH"


9. strripos (PHP 5)
strripos — Find position of last occurrence of a case-insensitive string in a string
Description
int strripos ( string $haystack, string $needle [, int $offset] )
Find position of last occurrence of a case-insensitive string in a string. Unlike strrpos(), strripos() is case-insensitive.
$haystack = ‘ababcd’;
$needle = ‘aB’;
$pos = strripos($haystack, $needle);
if ($pos === false) {
echo "Sorry, we did not find ($needle) in ($haystack)";
} else {
echo "Congratulations!n";
echo "We found the last ($needle) in ($haystack) at position ($pos)";
}


10. strrpos (PHP 4, PHP 5)
strrpos — Find position of last occurrence of a char in a string
Description
int strrpos ( string $haystack, string $needle [, int $offset] )
Returns the numeric position of the last occurrence of needle in the haystack string. Note that the needle in this case can only be a single character in PHP 4. If a string is passed as the needle, then only the first character of that string will be used.
If needle is not found, returns FALSE.
It is easy to mistake the return values for "character found at position 0" and "character not found". Here’s how to detect the difference:
// in PHP 4.0.0 and newer:
$pos = strrpos($mystring, "b");
if ($pos === false) { // note: three equal signs
// not found…
}
// in versions older than 4.0.0:
$pos = strrpos($mystring, "b");
if (is_bool($pos) && !$pos) {
// not found…
}


11. strstr (PHP 4, PHP 5)
strstr — Find first occurrence of a string
Description
string strstr ( string $haystack, string $needle, bool $before_needle )
Returns part of haystack string from the first occurrence of needle to the end of haystack.
Note: This function is case-sensitive. For case-insensitive searches, use stristr().
Note: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.


12. strtolower (PHP 4, PHP 5)
strtolower — Make a string lowercase
Description
string strtolower ( string $str )
Returns string with all alphabetic characters converted to lowercase.
Note that ‘alphabetic’ is determined by the current locale. This means that in i.e. the default "C" locale, characters such as umlaut-A (Ä) will not be converted.
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so


13. strtoupper (PHP 4, PHP 5)
strtoupper — Make a string uppercase
Description
string strtoupper ( string $string )
Returns string with all alphabetic characters converted to uppercase.
Note that ‘alphabetic’ is determined by the current locale. For instance, in the default "C" locale characters such as umlaut-a (ä) will not be converted.
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO


14. substr (PHP 4, PHP 5)
substr — Return part of a string
Description
string substr ( string $string, int $start [, int $length] )
Returns the portion of string specified by the start and length parameters.
$rest = substr("abcdef", -1); // returns "f"
$rest = substr("abcdef", -2); // returns "ef"
$rest = substr("abcdef", -3, 1); // returns "d"
echo substr(‘abcdef’, 1); // bcdef
echo substr(‘abcdef’, 1, 3); // bcd
echo substr(‘abcdef’, 0, 4); // abcd
echo substr(‘abcdef’, 0, 8); // abcdef
echo substr(‘abcdef’, -1, 1); // f

// Accessing single characters in a string
// can also be achived using "curly braces"
$string = ‘abcdef’;
echo $string{0}; // a
echo $string{3}; // d
echo $string{strlen($string)-1}; // f


15. substr_compare (PHP 5)
substr_compare — Binary safe comparison of 2 strings from an offset, up to length characters
Description
int substr_compare ( string $main_str, string $str, int $offset [, int $length [, bool $case_insensitivity]] )
substr_compare() compares main_str from position offset with str up to length characters.
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "de", -2, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning


16. substr_count (PHP 4, PHP 5)
substr_count — Count the number of substring occurrences
Description
int substr_count ( string $haystack, string $needle [, int $offset [, int $length]] )
substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive.
Note: This function doesn’t count overlapped substrings. See the example below!
$text = ‘This is a test’;
echo strlen($text); // 14
echo substr_count($text, ‘is’); // 2
echo substr_count($text, ‘is’, 3); // the string is reduced to ‘s is a test’, so it prints 1
echo substr_count($text, ‘is’, 3, 3); // the text is reduced to ‘s i’, so it prints 0
echo substr_count($text, ‘is’, 5, 10); // generates a warning because 5+10 > 14
$text2 = ‘gcdgcdgcd’; // prints only 1, because it doesn’t count overlapped subtrings


17. substr_replace (PHP 4, PHP 5)
substr_replace — Replace text within a portion of a string
Description
mixed substr_replace ( mixed $string, string $replacement, int $start [, int $length] )
substr_replace() replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.

$var = ‘ABCDEFGH:/MNRPQR/’;
echo "Original: $var


n";
/* These two examples replace all of $var with ‘bob’. */
echo substr_replace($var, ‘bob’, 0) . "
n";
echo substr_replace($var, ‘bob’, 0, strlen($var)) . "
n";
/* Insert ‘bob’ right at the beginning of $var. */
echo substr_replace($var, ‘bob’, 0, 0) . "
n";
/* These next two replace ‘MNRPQR’ in $var with ‘bob’. */
echo substr_replace($var, ‘bob’, 10, -1) . "
n";
echo substr_replace($var, ‘bob’, -7, -1) . "
n";
/* Delete ‘MNRPQR’ from $var. */
echo substr_replace($var, ”, 10, -1) . "
n";


18. ucfirst (PHP 4, PHP 5)
ucfirst — Make a string’s first character uppercase
Description
string ucfirst ( string $str )
Returns a string with the first character of str capitalized, if that character is alphabetic.
Note that ‘alphabetic’ is determined by the current locale. For instance, in the default "C" locale characters such as umlaut-a (ä) will not be converted.
$foo = ‘hello world!’;
$foo = ucfirst($foo); // Hello world!
$bar = ‘HELLO WORLD!’;
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!


19. ucwords (PHP 4, PHP 5)
ucwords — Uppercase the first character of each word in a string
Description
string ucwords ( string $str )
Returns a string with the first character of each word in str capitalized, if that character is alphabetic.
The definition of a word is any string of characters that is immediately after a whitespace (These are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab).
$foo = ‘hello world!’;
$foo = ucwords($foo); // Hello World!
$bar = ‘HELLO WORLD!’;
$bar = ucwords($bar); // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!


20. array_diff (PHP 4 >= 4.0.1, PHP 5)
array_diff — Computes the difference of arrays
Description
array array_diff ( array $array1, array $array2 [, array $ …] )
Compares array1 against array2 and returns the difference.
Examples
Example 248. array_diff() example

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);

One Response to “PHP Functions 1”


  1. ypudkis leahdn qvlgdpu sqgjalrp kide stkfmvcr agyjhbi

Leave a comment