0% found this document useful (0 votes)
57 views

PHP Strings Function: PHP: Indicates The Earliest Version of PHP That Supports The Function. Function Description

The document provides details about PHP string functions including their names, descriptions and examples. Some key functions discussed are addslashes(), explode(), strlen() and substr() which manipulate and retrieve information from strings.

Uploaded by

testing eng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

PHP Strings Function: PHP: Indicates The Earliest Version of PHP That Supports The Function. Function Description

The document provides details about PHP string functions including their names, descriptions and examples. Some key functions discussed are addslashes(), explode(), strlen() and substr() which manipulate and retrieve information from strings.

Uploaded by

testing eng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

PHP STRINGS FUNCTION

PHP: indicates the earliest version of PHP that supports the function.

Function Description

addcslashes() Returns a string with backslashes in front of the specified characters

Example

<?php
$str = "Hello, my name is Kai Jim.";
echo $str."<br />";
echo addcslashes($str,'m')."<br />";
echo addcslashes($str,'K')."<br />";
?>

OutputHello, my name is Kai Jim.


Hello, \my na\me is Kai Ji\m.
Hello, my name is \Kai Jim.

addslashes() Returns a string with backslashes in front of predefined characters

Example

<?php
$str = "Who's Kai Jim?";
echo $str . " This is not safe in a database query.<br />";
echo addslashes($str) . " This is safe in a database query.";
?>

OutputWho's Kai Jim? This is not safe in a database query.


Who\'s Kai Jim? This is safe in a database query.

bin2hex() Converts a string of ASCII characters to hexadecimal values

Example

<?php
$str = "a";
echo bin2hex($str) . "<br />";
echo pack("H*",’61’) . "<br />";
?>

Output 61
a

chop() Alias of rtrim()

Example

<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>

OutputHello World! Hello World!

chr() Returns a character from a specified ASCII value

Example

<?php
echo chr(52)."<br />";
echo chr(052)."<br />";
echo chr(0x52)."<br />";
?>

Output4 * R

chunk_split() Splits a string into a series of smaller parts

Example

<?php
$str = "Hello world!";
echo chunk_split($str,1,".");
?>

OutputH.e.l.l.o. .w.o.r.l.d.!.

convert_cyr_string() Converts a string from one Cyrillic character-set to another

Example

<?php
$str = "Hello world! æøå";
echo $str."<br />";
echo convert_cyr_string($str,'w','a');
?>

OutputHello world! æøå


Hello world! �.���

convert_uudecode() Decodes a uuencoded string

Example

<?php
$str = ",2&5L;&\@=V]R;&0A `";
echo convert_uudecode($str);
?>
OutputHello world!

convert_uuencode() Encodes a string using the uuencode algorithm

Example

<?php
$str = "Hello world!";
echo convert_uuencode($str);
?>

Output,2&5L;&\@=V]R;&0A `

count_chars() Returns how many times an ASCII character occurs within a string
and returns the information

Example

<?php

$str="aaaaabbbbcc";

print_r(count_chars($str,1));
?>

Output

Array ( [97] => 5 [98] => 4 [99] => 2 )

crc32() Calculates a 32-bit CRC for a string

Example

<?php
$str = crc32("Hello world!");
echo 'Without %u: '.$str."<br />";
echo 'With %u: ';
printf("%u",$str);
?>

Output

Without %u: 461707669


With %u: 461707669

One-way string encryption (hashing)

Example

<?php
if (CRYPT_STD_DES == 1){
crypt()
echo "Standard DES: ".crypt("hello world")."\n<br />";
}
else{
echo "Standard DES not supported.\n<br />";
}
if (CRYPT_EXT_DES == 1){
echo "Extended DES: ".crypt("hello world")."\n<br />";
}
else{
echo "Extended DES not supported.\n<br />";
}
if (CRYPT_MD5 == 1){
echo "MD5: ".crypt("hello world")."\n<br />";
}
else{
echo "MD5 not supported.\n<br />";
}
if (CRYPT_BLOWFISH == 1){
echo "Blowfish: ".crypt("hello world");
}
else{
echo "Blowfish DES not supported.";
}
?>

OutputStandard DES: $1$xc3.m61.$m64e6837acBtluUebIt3L.


Extended DES not supported.
MD5: $1$1i2..22.$Eko3fsmLl50.m5Ej8Sieh0
Blowfish DES not supported.

echo() Outputs strings

Example

<?php
$str = "Who's Kai Jim?";
echo $str;
echo "<br />";
echo $str."<br />I don't know!";
?>

OutputWho's Kai Jim?


Who's Kai Jim?
I don't know!

Breaks a string into an array

explode() Example

<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
OutputArray ( [0] => Hello [1] => world. [2] => It's [3] => a [4]
=> beautiful [5] => day. )

fprintf() Writes a formatted string to a specified output stream

Example

<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo fprintf($file,"%s world. Day number %u",$str,$number);
?>

Output27

get_html_translation_table() Returns the translation table used by htmlspecialchars() and


htmlentities()

Example

<?php
print_r (get_html_translation_table());
echo "<br />";
print_r (get_html_translation_table(HTML_ENTITIES));
?>

Output

Array ( ["] => " [<] =>< [>] =>> [&] =>& )
Array ( [�] => [�] => ¡ [�] => ¢ [�] => £ [�] => ¤ [�] => ¥
[�] => ¦ [�] => § [�] => ¨ [�] => © [�] => ª [�] => « [�] =>
¬ [�] => [�] => ® [�] => ¯ [�] => ° [�] => ± [�] => ² [�] =>
³ [�] => ´ [�] => µ [�] => ¶ [�] => · [�] => ¸ [�] => ¹ [�]
=>º [�] => » [�] => ¼ [�] => ½ [�] => ¾ [�] => ¿ [�] => À
[�] => Á [�] => Â [�] => Ã [�] => Ä [�] => Å [�] => Æ [�] =>
Ç [�] => È [�] => É [�] => Ê [�] => Ë [�] => Ì [�] => Í [�] =>
Î [�] => Ï [�] => Ð [�] => Ñ [�] => Ò [�] => Ó [�] => Ô [�]
=> Õ [�] => Ö [�] => × [�] => Ø [�] => Ù [�] => Ú [�] => Û
[�] => Ü [�] => Ý [�] => Þ [�] => ß [�] => à [�] => á [�] =>
â [�] => ã [�] => ä [�] => å [�] => æ [�] => ç [�] => è [�]
=> é [�] => ê [�] => ë [�] => ì [�] => í [�] => î [�] => ï [�]
=> ð [�] => ñ [�] => ò [�] => ó [�] => ô [�] => õ [�] => ö
[�] => ÷ [�] => ø [�] => ù [�] => ú [�] => û [�] => ü [�] =>
ý [�] => þ [�] => ÿ ["] => " [<] =>< [>] =>> [&] =>& )

hebrev() Converts Hebrew text to visual text

Example
<?php
echo hebrev("á çùåï äúùñâ");
?>

Output�á çùåï äúùñ�

hebrevc() Converts Hebrew text to visual text and new lines (\n) into <br />

Example

<?php
echo hebrevc("á çùåï äúùñâ\ná çùåï äúùñâ");
?>//Output�á çùåï äúùñ�
�á çùåï äúùñ�

Converts HTML entities to characters

Example

html_entity_decode() <?php
$str = "Jane &amp; &#039;Tarzan&#039;";
echo html_entity_decode($str);
echo "<br />";
echo html_entity_decode($str, ENT_QUOTES);
echo "<br />";
echo html_entity_decode($str, ENT_NOQUOTES);
?>

Output

Jane & 'Tarzan'


Jane & 'Tarzan'
Jane & 'Tarzan'

htmlentities() Converts characters to HTML entities

Example

<?php
$str = "Jane & 'Tarzan'";
echo htmlentities($str, ENT_COMPAT);
echo "<br />";
echo htmlentities($str, ENT_QUOTES);
echo "<br />";
echo htmlentities($str, ENT_NOQUOTES);
?>

OutputJane & 'Tarzan'


Jane & 'Tarzan'
Jane & 'Tarzan'

htmlspecialchars_decode() Converts some predefined HTML entities to characters


Example

<?php
$str = "Jane &amp; &#039;Tarzan&#039;";
echo htmlspecialchars_decode($str);
echo "<br />";
echo htmlspecialchars_decode($str, ENT_QUOTES);
echo "<br />";
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>

OutputJane & 'Tarzan'


Jane & 'Tarzan'
Jane & 'Tarzan'

htmlspecialchars() Converts some predefined characters to HTML entities

Example

<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT);
echo "<br />";
echo htmlspecialchars($str, ENT_QUOTES);
echo "<br />";
echo htmlspecialchars($str, ENT_NOQUOTES);
?>

Output

Jane & 'Tarzan'


Jane & 'Tarzan'
Jane & 'Tarzan'

Returns a string from the elements of an array

Example

implode() <?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

OutputHello World! Beautiful Day!

join() Alias of implode()

Example

<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);
?>

OutputHello World! Beautiful Day!

levenshtein() Returns the Levenshtein distance between two strings

Example

<?php
echo levenshtein("Hello World","ello World");
echo "<br />";
echo levenshtein("Hello World","ello World",10,20,30);
?>

Output

1
30

localeconv() Returns locale numeric and monetary formatting information

Example

<?php
setlocale(LC_ALL, 'US');
$locale_info = localeconv();
print_r($locale_info);
?>

Output

Array ( [decimal_point] => . [thousands_sep] => , [int_curr_symbol]


=> USD [currency_symbol] => $ [mon_decimal_point] => .
[mon_thousands_sep] => , [positive_sign] => [negative_sign] => -
[int_frac_digits] => 2 [frac_digits] => 2 [p_cs_precedes] => 1
[p_sep_by_space] => 0 [n_cs_precedes] => 1 [n_sep_by_space] =>
0 [p_sign_posn] => 3 [n_sign_posn] => 0 [grouping] => Array ( [0]
=> 3 ) [mon_grouping] => Array ( [0] => 3 ) )

ltrim() Strips whitespace from the left side of a string

Example

<?php
$str = " Hello World!";
echo "Without ltrim: " . $str;
echo "<br />";
echo "With ltrim: " . ltrim($str);
?>

Output
Without ltrim: Hello World!
With ltrim: Hello World!

md5() Calculates the MD5 hash of a string

Example

<?php
$str = "Hello";
echo md5($str);
?>

Output8b1a9953c4611296a827abf8c47804d7

md5_file() Calculates the MD5 hash of a file

Example

<?php
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>

Output8d75db5b6b096ff2096f895a57627b98

metaphone() Calculates the metaphone key of a string

Example

<?php
echo metaphone("world");
?>

OutputWRLT

money_format() Returns a string formatted as a currency string

Example

<?php
$number = 1234.56;
setlocale(LC_MONETARY, "en_US");
echo money_format("The price is %i", $number);
?>

OutputThe price is USD 1,234.56

Note: -The money_format() function does not work on Windows


platforms.

nl2br() Inserts HTML line breaks in front of each newline in a string


Example

<?php
echo nl2br("One line.\nAnother line.");
?>

Output

One line.
Another line.

number_format() Formats a number with grouped thousands

Example

<?php
echo number_format("1000000");
echo "<br />";
echo number_format("1000000",2);
echo "<br />";
echo number_format("1000000",2,",",".");
?>

Output

1,000,000
1,000,000.00
1.000.000,00

ord() Returns the ASCII value of the first character of a string

Example

<?php
echo ord("h")."<br />";
echo ord("hello")."<br />";
?>

Output

104
104

Parses a query string into variables

parse_str() Example

<?php
parse_str("id=23&name=Kai%20Jim");
echo $id."<br />";
echo $name;
?>
Output23
Kai Jim

print() Outputs a string

Example

<?php

$str = "Who's Kai Jim?";

print $str;

print "<br />";

print $str."<br />I don't know!";

?>

Output

Who's Kai Jim?


Who's Kai Jim?
I don't know!

printf() Outputs a formatted string

Example

<?php
$str = "Hello";
$number = 123;
printf("%s world. Day number %u",$str,$number);
?>

OutputHello world. Day number 123

quoted_printable_decode() Decodes a quoted-printable string

Example

<?php
$str = "Hello=0Aworld.";
echo quoted_printable_decode($str);
?>

OutputHello world.

quotemeta() Quotes meta characters

Example

<?php
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
?>

OutputHello world\. \(can you hear me\?\)

rtrim() Strips whitespace from the right side of a string

Example

<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br />";
echo "With rtrim: " . rtrim($str);
?>

OutputWithout rtrim: Hello World!


With rtrim: Hello World!

Sets locale information

Example

setlocale() <?php
echo setlocale(LC_ALL,"En-Us");
echo "<br />";
echo setlocale(LC_ALL,NULL);
?>

OutputEnglish_United States.1252

sha1() Calculates the SHA-1 hash of a string

Example

<?php

$str = 'Hello';

echo sha1($str);

?>

Outputf7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0

sha1_file() Calculates the SHA-1 hash of a file

Example

<?php

$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
similar_text() Calculates the similarity between two strings

Example
<?php
echo similar_text("Hello World","Hello Peter");
?>

Output 7

soundex() Calculates the soundex key of a string

Example

<?php
$str = "hello";
echo soundex($str);
?>

OutputH400

sprintf() Writes a formatted string to a variable

Example

<?php
$str = "Hello";
$number = 123;
$txt = sprintf("%s world. Day number %u",$str,$number);
echo $txt;
?>//OutputHello world. Day number 123

sscanf() Parses input from a string according to a format

Example

<?php
$string = "age:30 weight:60kg";
sscanf($string,"age:%d weight:%dkg",$age,$weight);
// show types and values
var_dump($age,$weight);
?>

Outputint(30) int(60)

str_ireplace() Replaces some characters in a string (case-insensitive)

Example

<?php
echo str_ireplace("WORLD","Peter","Hello world!");
?>

OutputHello Peter!
str_pad() Pads a string to a new length

Example

<?php
$str = "Hello World";
echo str_pad($str,20,".");
?>

OutputHello World.........

str_repeat() Repeats a string a specified number of times

Example

<?php
echo str_repeat("p",13);
?>

Outputppppppppppppp

str_replace() Replaces some characters in a string (case-sensitive)

Example

<?php
echo str_replace("world","Peter","Hello world!");
?>

OutputHello Peter!

str_rot13() Performs the ROT13 encoding on a string

Example

<?php
echo str_rot13("Hello World");
echo "<br />";
echo str_rot13("Uryyb Jbeyq");
?>

OutputUryyb Jbeyq
Hello World
str_shuffle() Randomly shuffles all characters in a string

Example

<?php
echo str_shuffle("Hello World");
?>
str_split() Splits a string into an array

Example
<?php
print_r(str_split("Hello"));
?>

OutputArray ( [0] => H [1] => e [2] => l [3] => l [4] => o )
str_word_count() Count the number of words in a string
Example
<?php
echo str_word_count("Hello world!");
?>

Output 2
Compares two strings (case-insensitive)

Example

strcasecmp() <?php
echo strcasecmp("Hello world!","HELLO WORLD!");
?>

Output 0

strchr() Finds the first occurrence of a string inside another string (alias of
strstr())

Example

<?php
echo strchr("Hello world!","world");
?>

Outputworld!

strcmp() Compares two strings (case-sensitive)

Example

<?php
echo strcmp("Hello world!","Hello world!");
?>

Output 0

strcoll() Locale based string comparison

Example

<?php
setlocale (LC_COLLATE, 'NL');
echo strcoll("Hello World!","Hello WORLD!");
echo "<br />";
setlocale (LC_COLLATE, 'en_US');
echo strcoll("Hello World!","Hello WORLD!");
?>

Output:

1
1

strcspn() Returns the number of characters found in a string before any part of
some specified characters are found

Example

<?php
echo strcspn("Hello world!","w");
?>

Output 6

strip_tags() Strips HTML and PHP tags from a string

Example

<?php
echo strip_tags("Hello <b>world!</b>");
?>

OutputHello world!

stripcslashes() Unquotes a string quoted with addcslashes()

Example

<?php
echo stripcslashes("Hello, \my na\me is Kai Ji\m.");
?>

OutputHello, my name is Kai Jim.

stripslashes() Unquotes a string quoted with addslashes()

Example

<?php
echo stripslashes("Who\'s Kai Jim?");
?>

OutputWho's Kai Jim?

stripos() Returns the position of the first occurrence of a string inside another
string (case-insensitive)

Example
<?php
echo stripos("Hello world!","WO");
?>

Output 6

stristr() Finds the first occurrence of a string inside another string (case-
insensitive)

Example

<?php
echo stristr("Hello world!","WORLD");
?>

Outputworld!

strlen() Returns the length of a string

Example

<?php
echo strlen("Hello world!");
?>

Output 12

strnatcasecmp() Compares two strings using a "natural order" algorithm (case-


insensitive)

Example

<?php
echo strnatcasecmp("2Hello world!","10Hello world!");
echo "<br />";
echo strnatcasecmp("10Hello world!","2Hello world!");
?>

Output-1
1

strnatcmp() Compares two strings using a "natural order" algorithm (case-


sensitive)

Example

<?php

echo strnatcasecmp("2Hello world!","10Hello world!");

echo "<br />";

echo strnatcasecmp("10Hello world!","2Hello world!");


?>

Output

-1

strncasecmp() String comparison of the first n characters (case-insensitive)

Example

<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>

Output 0

strncmp() String comparison of the first n characters (case-sensitive)

Example

<?php
echo strncmp("Hello world!","Hello earth!",6);
?>

Output 0

Searches a string for any of a set of characters

Example

<?php
echo strpbrk("Hello world!","oe");
strpbrk()
?>

Outputello world!

strpos() Returns the position of the first occurrence of a string inside another
string (case-sensitive)

Example

<?php
echo strpos("Hello world!","wo");
?>

Output 6

strrchr() Finds the last occurrence of a string inside another string

Example
<?php
echo strrchr("Hello world!","world");
?>

Outputworld!

strrev() Reverses a string

Example

<?php
echo strrev("Hello World!");
?>

Output!dlroW olleH

strripos() Finds the position of the last occurrence of a string inside another
string (case-insensitive)

Example

<?php
echo strripos("Hello world!","WO");
?>

Output 6

strrpos() Finds the position of the last occurrence of a string inside another
string (case-sensitive)

Example

<?php
echo strrpos("Hello world!","wo");
?>

Output 6

strspn() Returns the number of characters found in a string that contains only
characters from a specified charlist

Example

<?php
echo strspn("Hello world!","kHlleo");
?>

Output 5

strstr() Finds the first occurrence of a string inside another string (case-
sensitive)

Example
<?php
echo strstr("Hello world!","world");
?>

Outputworld!

Splits a string into smaller strings

Example

strtok() <?php
$string = "Hello world. Beautiful day today.";
$token = strtok($string, " ");
while ($token != false)
{
echo "$token<br />";
$token = strtok(" ");
}

?>

OutputHello
world.
Beautiful
day
today.

strtolower() Converts a string to lowercase letters

Example

<?php
echo strtolower("Hello WORLD.");
?>

Outputhello world.

strtoupper() Converts a string to uppercase letters

Example

<?php
echo strtoupper("Hello WORLD!");
?>

OutputHELLO WORLD!

strtr() Translates certain characters in a string

Example
<?php
echo strtr("Hilla Warld","ia","eo");
?>

OutputHello World

substr() Returns a part of a string

Example

<?php
echo substr("Hello world!",6);
?>//Outputworld!

substr_compare() Compares two strings from a specified start position (binary safe and
optionally case-sensitive)

Example

<?php
echo substr_compare("Hello world","Hello world",0);
?>

Output 0

substr_count() Counts the number of times a substring occurs in a string

Example

<?php
echo substr_count("Hello world. The world is nice","world");

?>

Output 2

substr_replace() Replaces a part of a string with another string

Example

<?php
echo substr_replace("Hello world","earth",6);
?>

OutputHello earth

trim() Strips whitespace from both sides of a string

Example

<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br />";
echo "With trim: " . trim($str);
?>

OutputWithout trim: Hello World!


With trim: Hello World!

ucfirst() Converts the first character of a string to uppercase

Example

<?php
echo ucfirst("hello world");
?>

OutputHello world

ucwords() Converts the first character of each word in a string to uppercase

Example

<?php
echo ucwords("hello world");
?>

OutputHello World

vfprintf() Writes a formatted string to a specified output stream

Example

<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo vfprintf($file,"%s world. Day number
%u",array($str,$number));
?>

Output 27

(test.txt will be written “Hello world. Day number 123”)

vprintf() Outputs a formatted string

Example

<?php
$str = "Hello";
$number = 123;
vprintf("%s world. Day number %u",array($str,$number));
?>

OutputHello world. Day number 123


vsprintf() Writes a formatted string to a variable

Example

<?php
$str = "Hello";
$number = 123;
$txt = vsprintf("%s world. Day number %u",array($str,$number));
echo $txt;
?>

OutputHello world. Day number 123

wordwrap() Wraps a string to a given number of characters

Example

<?php
$str = "An example on a long word is: Supercalifragulistic";
echo wordwrap($str,15);
?>

OutputAn example on a long word is: Supercalifragulistic

You might also like