Strings Flashcards
Returns a string with backslashes in front of the specified characters
addcslashes(string,characters) --------------- EXAMPLE: $str = addcslashes("Hello World!","W"); echo($str);
OUTPUT:
Hello \World!
Returns a string with backslashes in front of predefined characters
addslashes(string) --------------- EXAMPLE: $str = addslashes('What does "yolo" mean?'); echo($str);
OUTPUT:
What does "yolo" mean?
Converts a string of ASCII characters to hexadecimal values
bin2hex(string) --------------- EXAMPLE: $str = bin2hex("Hello World!"); echo($str);
OUTPUT:
48656c6c6f20576f726c6421
Removes whitespace or other characters from the right end of a string
chop(string,charlist) --------------- EXAMPLE: $str = "Hello World!"; echo $str . "<br>"; echo chop($str,"World!");
OUTPUT:
Hello World!
Hello
Returns a character from a specified ASCII value
chr(ascii) --------------- EXAMPLE: echo chr(52) . "<br>"; // Decimal value echo chr(052) . "<br>"; // Octal value echo chr(0x52) . "<br>"; // Hex value
OUTPUT:
4
*
R
Splits a string into a series of smaller parts
chunk_split(string,length,end) --------------- EXAMPLE: $str = "Hello world!"; echo chunk_split($str,1,".");
OUTPUT:
H.e.l.l.o. .w.o.r.l.d.!.
Decodes a uuencoded string
convert_uudecode(string) --------------- EXAMPLE: $str = ",2&5L;&\@=V]R;&0A `"; echo convert_uudecode($str);
OUTPUT:
Hello world!
Encodes a string using the uuencode algorithm
convert_uuencode(string) --------------- EXAMPLE: $str = "Hello world!"; echo convert_uuencode($str);
OUTPUT:
,2&5L;&\@=V]R;&0A `
Returns information about characters used in a string
count_chars(string,mode) --------------- EXAMPLE: $str = "Hello World!"; echo count_chars($str,3);
OUTPUT:
The parameter “mode 3” will return a string with all the different characters used. In this example, the characters used in “Hello World!” are:
!HWdelor
Outputs one or more strings
EXAMPLE:
echo “Hello world!”;
OUTPUT:
Hello world!
Breaks a string into an array
EXAMPLE:
$str = ‘one,two,three,four’;
// zero limit print_r(explode(',',$str,0)); print "<br>";
// positive limit print_r(explode(',',$str,2)); print "<br>";
// negative limit print_r(explode(',',$str,-1));
OUTPUT:
Array ( [0] => one,two,three,four )
Array ( [0] => one [1] => two,three,four )
Array ( [0] => one [1] => two [2] => three )
Writes a formatted string to a specified output stream
fprintf(stream,format,arg1,arg2,arg++) --------------- EXAMPLE: $number = 9; $str = "Beijing"; $file = fopen("test.txt","w"); echo fprintf($file,"There are %u million bicycles in %s.",$number,$str);
OUTPUT:
40
The following text will be written to the file “test.txt”:
There are 9 million bicycles in Beijing.
Converts a string of hexadecimal values to ASCII characters
EXAMPLE:
echo hex2bin(“48656c6c6f20576f726c6421”);
OUTPUT:
Hello World!
Converts HTML entities to characters
html_entity_decode(string,flags,character-set) --------------- EXAMPLE: Jane & 'Tarzan'<br> Jane & 'Tarzan'<br> Jane & 'Tarzan'
OUTPUT:
Jane & ‘Tarzan’
Jane & ‘Tarzan’
Jane & ‘Tarzan’
Converts characters to HTML entities
EXAMPLE:
$str = “Jane & ‘Tarzan’”;
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes
echo “<br></br>”;
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes
echo “<br></br>”;
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes
OUTPUT:
Jane & ‘Tarzan’
Jane & ‘Tarzan’
Jane & ‘Tarzan’
Converts some predefined HTML entities to characters
htmlspecialchars_decode(string,flags) --------------- EXAMPLE: $str = "This is some <b>bold</b> text."; echo htmlspecialchars_decode($str);
OUTPUT:
This is some bold text.
Converts some predefined characters to HTML entities
htmlspecialchars(string,flags,character-set,double_encode)
The predefined characters are:
& (ampersand) becomes & " (double quote) becomes " ' (single quote) becomes ' (greater than) becomes > --------------- EXAMPLE: $str = "This is some <b>bold</b> text."; echo htmlspecialchars($str);
OUTPUT:
This is some <b>bold</b> text.
Returns a string from the elements of an array
implode(separator,array) --------------- EXAMPLE: $arr = array('Hello','World!','Beautiful','Day!'); echo implode(" ",$arr);
OUTPUT:
Hello World! Beautiful Day!
Alias of implode()
join(separator,array) --------------- EXAMPLE: $arr = array('Hello','World!','Beautiful','Day!'); echo join(" ",$arr);
OUTPUT:
Hello World! Beautiful Day!
Converts the first character of a string to lowercase
EXAMPLE:
echo lcfirst(“Hello world!”);
OUTPUT:
hello world!
Returns locale numeric and monetary formatting information
localeconv() -------------- EXAMPLE: 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 ) )
Removes whitespace or other characters from the left side of a string
ltrim(string,charlist) -------------- EXAMPLE: $str = "Hello World!"; echo $str . "<br>"; echo ltrim($str,"Hello");
OUTPUT:
Hello World!
World!
Returns a string formatted as a currency string
money_format(string,number) -------------- EXAMPLE: $number = 1234.56; setlocale(LC_MONETARY,"en_US"); echo money_format("The price is %i", $number);
OUTPUT:
The price is USD 1,234.56
Inserts HTML line breaks in front of each newline in a string
EXAMPLE:
echo nl2br(“One line.\nAnother line.”);
OUTPUT:
One line.
Another line.
Formats a number with grouped thousands
number_format(number,decimals,decimalpoint,separator) --------------- EXAMPLE: echo number_format("1000000")."<br>"; echo number_format("1000000",2)."<br>"; echo number_format("1000000",2,",",".");
OUTPUT:
1,000,000
1,000,000.00
1.000.000,00