Categories:




URL.biz - Education


PHP Tutorial !



PHP String functions

Rank:

String functions in PHP

Substr() function - Gets part of a string

string substr(string, start[, length]);

Example:
echo substr('Hello World!', 5, 5); // Output : "World"

strlen() - Length of a string

strlen(stringname);

Example:
strlen("Mica Jovic"); Output : 10

is_integer(), is_string(), is_double(), - Verifying data type

Example:
$i = 10;

if (is_integer($i))
{
   echo("$i is an integer");
}

if (is_string($i))
{
   echo("$i is a string");
}

if (is_double($i))
{
   echo("$i is a double");
}

Trim(), ltrim() and rtrim() function

trim() - Removes all whitespace characters from both the left and right ends of a string.
ltrim() - Removes all whitespace characters from left end of string
rtrim() - Removes all whitespace characters from right ends of a string.

whitespace characters: space, null, newline, tab, carriage return!

Example:
$strText = " Hello World ";

$strNew = trim($strText); // Output : "Hello World"
$strNew = ltrim($strText); // Output : "Hello World "
$strNew = rtrim($strText); // Output : " Hello World"

explode() - Splitting a string into an array

explode(separator,stringname);

Example:
$string="11-03-2007"

$newArray = explode("-",$string);

echo $newArray[0] Output : 11
echo $newArray[1] Output : 03
echo $newArray[2] Output : 2007

join() or implode() - Joining the array elements into a single string

join(separator,arrayname);
      (Or)
implode(separator,arrayname);

Example:
$oldArray=array("03","02","2007");

implode("/",$oldArray); Output : 03/02/2007

str_repeat() - Repeatting a string 'n' times

str_repeat($string,integervalue);

Example:
str_repeat("122",3); Output : 123123123

strip_tags() - Remove HTML tags from the string

strip_tags($string, [allowable tags]);

Example:

strip_tags("<i>Hello World</i> <br> <b>Hello World</b>","<br>");
// Output : "Hello World Hello<br> World<br>"

str_replace() function — Find and replace part of the string

str_replace(find, replace, string);

Example:
echo str_replace(".", "!", "Hello World!"); // Output : "Hello World."

checkdate() - Check the date format

checkdate ($intMonth,$date, $intYear);

1. checkdate (1, 30,2007); Output: (1 means true)

2. checkdate (2, 30,2007); Output: (null means false)


Top Tutorials:

Basic HTML Tags

Now we are going to start talking about HTML tags...


PHP - String functions
If you’d like to know how to manipulate with strings in PHP...