PHP Strings Function: PHP: Indicates The Earliest Version of PHP That Supports The Function. Function Description
PHP Strings Function: PHP: Indicates The Earliest Version of PHP That Supports The Function. Function Description
PHP: indicates the earliest version of PHP that supports the function.
Function Description
Example
<?php
$str = "Hello, my name is Kai Jim.";
echo $str."<br />";
echo addcslashes($str,'m')."<br />";
echo addcslashes($str,'K')."<br />";
?>
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.";
?>
Example
<?php
$str = "a";
echo bin2hex($str) . "<br />";
echo pack("H*",’61’) . "<br />";
?>
Output 61
a
Example
<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
Example
<?php
echo chr(52)."<br />";
echo chr(052)."<br />";
echo chr(0x52)."<br />";
?>
Output4 * R
Example
<?php
$str = "Hello world!";
echo chunk_split($str,1,".");
?>
OutputH.e.l.l.o. .w.o.r.l.d.!.
Example
<?php
$str = "Hello world! æøå";
echo $str."<br />";
echo convert_cyr_string($str,'w','a');
?>
Example
<?php
$str = ",2&5L;&\@=V]R;&0A `";
echo convert_uudecode($str);
?>
OutputHello world!
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
Example
<?php
$str = crc32("Hello world!");
echo 'Without %u: '.$str."<br />";
echo 'With %u: ';
printf("%u",$str);
?>
Output
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.";
}
?>
Example
<?php
$str = "Who's Kai Jim?";
echo $str;
echo "<br />";
echo $str."<br />I don't know!";
?>
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. )
Example
<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo fprintf($file,"%s world. Day number %u",$str,$number);
?>
Output27
Example
<?php
print_r (get_html_translation_table());
echo "<br />";
print_r (get_html_translation_table(HTML_ENTITIES));
?>
Output
Array ( ["] => " [<] =>< [>] =>> [&] =>& )
Array ( [�] => [�] => ¡ [�] => ¢ [�] => £ [�] => ¤ [�] => ¥
[�] => ¦ [�] => § [�] => ¨ [�] => © [�] => ª [�] => « [�] =>
¬ [�] => [�] => ® [�] => ¯ [�] => ° [�] => ± [�] => ² [�] =>
³ [�] => ´ [�] => µ [�] => ¶ [�] => · [�] => ¸ [�] => ¹ [�]
=>º [�] => » [�] => ¼ [�] => ½ [�] => ¾ [�] => ¿ [�] => À
[�] => Á [�] => Â [�] => Ã [�] => Ä [�] => Å [�] => Æ [�] =>
Ç [�] => È [�] => É [�] => Ê [�] => Ë [�] => Ì [�] => Í [�] =>
Î [�] => Ï [�] => Ð [�] => Ñ [�] => Ò [�] => Ó [�] => Ô [�]
=> Õ [�] => Ö [�] => × [�] => Ø [�] => Ù [�] => Ú [�] => Û
[�] => Ü [�] => Ý [�] => Þ [�] => ß [�] => à [�] => á [�] =>
â [�] => ã [�] => ä [�] => å [�] => æ [�] => ç [�] => è [�]
=> é [�] => ê [�] => ë [�] => ì [�] => í [�] => î [�] => ï [�]
=> ð [�] => ñ [�] => ò [�] => ó [�] => ô [�] => õ [�] => ö
[�] => ÷ [�] => ø [�] => ù [�] => ú [�] => û [�] => ü [�] =>
ý [�] => þ [�] => ÿ ["] => " [<] =>< [>] =>> [&] =>& )
Example
<?php
echo hebrev("á çùåï äúùñâ");
?>
hebrevc() Converts Hebrew text to visual text and new lines (\n) into <br />
Example
<?php
echo hebrevc("á çùåï äúùñâ\ná çùåï äúùñâ");
?>//Output�á çùåï äúùñ�
�á çùåï äúùñ�
Example
html_entity_decode() <?php
$str = "Jane & 'Tarzan'";
echo html_entity_decode($str);
echo "<br />";
echo html_entity_decode($str, ENT_QUOTES);
echo "<br />";
echo html_entity_decode($str, ENT_NOQUOTES);
?>
Output
Example
<?php
$str = "Jane & 'Tarzan'";
echo htmlentities($str, ENT_COMPAT);
echo "<br />";
echo htmlentities($str, ENT_QUOTES);
echo "<br />";
echo htmlentities($str, ENT_NOQUOTES);
?>
<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars_decode($str);
echo "<br />";
echo htmlspecialchars_decode($str, ENT_QUOTES);
echo "<br />";
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>
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
Example
implode() <?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
Example
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);
?>
Example
<?php
echo levenshtein("Hello World","ello World");
echo "<br />";
echo levenshtein("Hello World","ello World",10,20,30);
?>
Output
1
30
Example
<?php
setlocale(LC_ALL, 'US');
$locale_info = localeconv();
print_r($locale_info);
?>
Output
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!
Example
<?php
$str = "Hello";
echo md5($str);
?>
Output8b1a9953c4611296a827abf8c47804d7
Example
<?php
$filename = "test.txt";
$md5file = md5_file($filename);
echo $md5file;
?>
Output8d75db5b6b096ff2096f895a57627b98
Example
<?php
echo metaphone("world");
?>
OutputWRLT
Example
<?php
$number = 1234.56;
setlocale(LC_MONETARY, "en_US");
echo money_format("The price is %i", $number);
?>
<?php
echo nl2br("One line.\nAnother line.");
?>
Output
One line.
Another line.
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
Example
<?php
echo ord("h")."<br />";
echo ord("hello")."<br />";
?>
Output
104
104
parse_str() Example
<?php
parse_str("id=23&name=Kai%20Jim");
echo $id."<br />";
echo $name;
?>
Output23
Kai Jim
Example
<?php
print $str;
?>
Output
Example
<?php
$str = "Hello";
$number = 123;
printf("%s world. Day number %u",$str,$number);
?>
Example
<?php
$str = "Hello=0Aworld.";
echo quoted_printable_decode($str);
?>
OutputHello world.
Example
<?php
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
?>
Example
<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br />";
echo "With rtrim: " . rtrim($str);
?>
Example
setlocale() <?php
echo setlocale(LC_ALL,"En-Us");
echo "<br />";
echo setlocale(LC_ALL,NULL);
?>
OutputEnglish_United States.1252
Example
<?php
$str = 'Hello';
echo sha1($str);
?>
Outputf7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
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
Example
<?php
$str = "hello";
echo soundex($str);
?>
OutputH400
Example
<?php
$str = "Hello";
$number = 123;
$txt = sprintf("%s world. Day number %u",$str,$number);
echo $txt;
?>//OutputHello world. Day number 123
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)
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.........
Example
<?php
echo str_repeat("p",13);
?>
Outputppppppppppppp
Example
<?php
echo str_replace("world","Peter","Hello world!");
?>
OutputHello Peter!
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!
Example
<?php
echo strcmp("Hello world!","Hello world!");
?>
Output 0
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
Example
<?php
echo strip_tags("Hello <b>world!</b>");
?>
OutputHello world!
Example
<?php
echo stripcslashes("Hello, \my na\me is Kai Ji\m.");
?>
Example
<?php
echo stripslashes("Who\'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!
Example
<?php
echo strlen("Hello world!");
?>
Output 12
Example
<?php
echo strnatcasecmp("2Hello world!","10Hello world!");
echo "<br />";
echo strnatcasecmp("10Hello world!","2Hello world!");
?>
Output-1
1
Example
<?php
Output
-1
Example
<?php
echo strncasecmp("Hello world!","hello earth!",6);
?>
Output 0
Example
<?php
echo strncmp("Hello world!","Hello earth!",6);
?>
Output 0
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
Example
<?php
echo strrchr("Hello world!","world");
?>
Outputworld!
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!
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.
Example
<?php
echo strtolower("Hello WORLD.");
?>
Outputhello world.
Example
<?php
echo strtoupper("Hello WORLD!");
?>
OutputHELLO WORLD!
Example
<?php
echo strtr("Hilla Warld","ia","eo");
?>
OutputHello World
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
Example
<?php
echo substr_count("Hello world. The world is nice","world");
?>
Output 2
Example
<?php
echo substr_replace("Hello world","earth",6);
?>
OutputHello earth
Example
<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br />";
echo "With trim: " . trim($str);
?>
Example
<?php
echo ucfirst("hello world");
?>
OutputHello world
Example
<?php
echo ucwords("hello world");
?>
OutputHello World
Example
<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo vfprintf($file,"%s world. Day number
%u",array($str,$number));
?>
Output 27
Example
<?php
$str = "Hello";
$number = 123;
vprintf("%s world. Day number %u",array($str,$number));
?>
Example
<?php
$str = "Hello";
$number = 123;
$txt = vsprintf("%s world. Day number %u",array($str,$number));
echo $txt;
?>
Example
<?php
$str = "An example on a long word is: Supercalifragulistic";
echo wordwrap($str,15);
?>