* Returns a string with backslashes before characters such as single quote ('), double quote ("), backslash (\) and NULL (the NULL byte).
* This is mainly used for escaping database queries.
* This function complements stripslashes().
<?php $str = "What's your name?"; echo addslashes($str); $str = "Is your name O'reilly??"; echo "
".addslashes($str); ?>
Returns a string with backslashes stripped off.(\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).)
<?php $str = "What\'s your name?"; echo stripslashes($str); $str = "Is your name O\'reilly?"; echo "
".stripslashes($str); ?>
* Returns a one-character string containing the character specified by ascii.
* This function complements ord().
<?php echo chr(65); echo "
".chr(98); ?>
Returns ascii value of first character of string
<?php echo ord("Preeti"); echo ord("b"); ?>
* This function returns a string with stripped characters specified using the charlist parameter from the beginning and end of string.
* Without the second parameter, trim() will strip these characters: ' ' (ASCII 32 (0x20)), an ordinary space. '\t' (ASCII 9 (0x09)), a tab. '\n' (ASCII 10 (0x0A)), a new line (line feed).
<?php $str = "\t\tThese are a few words :) ... "; echo trim($str); $str = trim($str); echo trim($str,'.'); ?>
* This functions returns the input string padded on the left, the right, or both sides to the specified padding length and padded with characters from pad_string(if not supplied then spaces) up to the limit.
* Parameters:
input - The input string.
<?php $input = "Afixi"; echo str_pad($input, 10); echo str_pad($input, 10, "-=", STR_PAD_LEFT); echo str_pad($input, 10, ".", STR_PAD_BOTH); echo str_pad($input, 6 , "___"); ?>
* This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.
* If search or replace are arrays, their elements are processed first to last.
* If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.
* Count if passed, this will hold the number of matched and replaced needles.
<?php $search='x'; $replaceWith='php'; $str='x-myadmin'; $result=str_replace($search, $replaceWith, $str, $count); print $result; print $count; ?>
* Returns part of haystack string from the first occurrence of needle to the end of haystack after a case-sensitive search.
* If needle not found returns false.
* Parameters :
-> haystack : (Required)The input string.
-> needle : (Required)If not a string, it is converted to an integer and applied as the ordinal value of a character.
-> before_needle :(Optional) If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle). By default it is FALSE.
* For case-insensitive searches, use stristr().
<?php $email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain; echo "
".strstr("Hello world!",111); //In this example we will search a string for the ASCII value of "o": ?>
* This function returns a string with all HTML and PHP tags stripped from a given strings.
<?php echo strip_tags("Hello world!"); echo "
"; echo strip_tags("Hello world!","<b>"); ?>
Hello world!
Hello world!
Returns the length of the given string.
<?php $str = 'abcdef'; echo strlen($str); $str = 'abc def'; echo "
".strlen($str); ?>
* Returns string with all alphabetic characters converted to lowercase.
* This function complements strtoupper().
Returns string with all alphabetic characters converted to uppercase.
<?php $str = "Mary Had A Little Lamb and She LOVED It So"; $str = strtolower($str); echo $str; echo "
".strtoupper($str); ?>
Returns the portion of string specified by the start and length parameters.
If start is non-negative, the returned string will start at the start'th position in string, counting from zero.
If start is negative, the returned string will start at the start'th character from the end of string.
<?php $rest = substr("abcdef", 1);echo $rest; $rest = substr("abcdef", 1, 3);echo $rest; $rest = substr("abcdef", 0, 4);echo $rest; $rest = substr("abcdef", 0, 8);echo $rest; //Using Negative Start: $rest = substr("abcdef", -1);echo $rest; $rest = substr("abcdef", -2);echo $rest; $rest = substr("abcdef", -3, 1);echo $rest; ?>
Returns a string with the first character of string capitalized, if that character is alphabetic.
<?php $foo = 'hello world!'; echo $foo; echo ucfirst($foo); $bar = 'HELLO WORLD!'; echo $bar; $bar = ucfirst($bar); $bar = ucfirst(strtolower($bar)); echo $bar; ?>
Returns a string with the first character of each word in string capitalized, if that character is alphabetic.
<?php $foo = 'hello world!'; echo $foo; $foo = ucwords($foo); echo $foo; $bar = 'HELLO WORLD!'; $bar = ucwords($bar); $bar = ucwords(strtolower($bar)); echo $bar; ?>
Binary safe string comparison
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.Comparison is case sensitive.
For case-insensitive comparison use strcasecmp()(Syntax:int strcasecmp(string str1,string str2))
<?php $var1 = "Hello"; $var2 = "hello"; if (strcmp($var1, $var2) == 0) { echo '$var1 is equal to $var2 in a case-sensitive string comparison'; } else { echo '$var1 is not equal to $var2 in a case-sensitive string comparison'; } ?>
Returns the reversed string.
<?php $str = "Hello world!"; echo strrev($str); ?>
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
<?php $num_str ="one,two,three"; $num = explode(",", $num_str); echo "<pre>"; print_r($num); $num_str ="This is cow"; $num = explode(" ", $num_str); print_r($num); ?>
Array ( [0] => one [1] => two [2] => three ) Array ( [0] => This [1] => is [2] => cow )
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.
<?php $detail=Array("Afixi","Technologies",9853247533); $detail_string=implode(",",$detail); print $detail_string; echo "
"; $detail=Array("Afixi","Technologies","Pvt.","Ltd."); $detail_string=implode(" ",$detail); print $detail_string; ?>
Find position of first occurrence of a string
<?php $mystring = 'abc'; $findme = 'b'; $pos = strpos($mystring, $findme); 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"; } ?>
Wraps a string to a given number of characters
<?php $str = "An example on a long word is: Supercalifragulistic"; echo wordwrap($str,15,"
\n",TRUE); ?>
After editing Click here: |
Output:
|
Hello World
|