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’