View Category
Output a string to the console
Write the string
"Hello World!" to STDOUT
php
echo 'Hello World!';
/****
* For some (security)reason I couldn't
* submit this without adding a space to
* the functionname. Please remove it :)
****/
// The correct way in command line-mode. :)
f write(STDOUT, "Hello World!\n");
* For some (security)reason I couldn't
* submit this without adding a space to
* the functionname. Please remove it :)
****/
// The correct way in command line-mode. :)
f write(STDOUT, "Hello World!\n");
Retrieve a string containing ampersands from the variables in a url
My PHP script first does a query to obtain customer info for a form. The form has first name and last name fields among others. The customer has put entries such as
The script variable for first name $_REQUEST
I have tried various functions like urldecode but all to no avail. I even tried encoding the url before the view screen is painted so that the url looks like
Of course this fails for the same reasons. What is a better approach?
"Ron & Jean" in the first name field in the database. Then the edit form script is called with variables such as
"http://myserver.com/custinfo/edit.php?mode=view&fname=Ron & Jean&lname=Smith".
The script variable for first name $_REQUEST
['firstname'] never gets beyond the "Ron" value because of the ampersand in the data.
I have tried various functions like urldecode but all to no avail. I even tried encoding the url before the view screen is painted so that the url looks like
"http://myserver/custinfo/edit.php?mode=view&fname="Ronxxnbsp;xxamp;xxnbsp;Jean"&lname=SMITH". (sorry I had to add the xx to replace the ampersand or it didn't display meaningful url contents the browser sees.)
Of course this fails for the same reasons. What is a better approach?
php
echo("The given input URL is mal-formed; it should be URL-encoded. Use urldecode() on each argument to decode it.");
$url = parse_url("http://myserver.com/custinfo/edit.php?mode=view&fname=Ron & Jean&lname=Smith");
$query = $url['query'];
$query = preg_replace("/&(?=[^=]*&)/", "%26", $query);
parse_str($query, $querystring);
var_dump($querystring);
the result is >> array(3) {
["mode"]=>
string(4) "view"
["fname"]=>
string(10) "Ron & Jean"
["lname"]=>
string(5) "Smith"
}
/************************
* This is definitely not what this site is about
* This is not a site where you can get help with various problems
* It's a site where you can say:
* "How do you do the following in your language"
*
*************
*
* Anyway...
* I would use urlencode like you have tried
* (or to be on the safe side: rawurlencode() )
************************/
$parameter = "Bart & Lisa";
echo "<a href='".basename(__FILE__)."?fname=".$parameter."'>?fname=".$parameter."</a><br />\n";
echo "<a href='".basename(__FILE__)."?fname=".rawurlencode($parameter)."'>?fname=".rawurlencode($parameter)."</a><br />\n";
echo "<pre>", print_r($_REQUEST, true), "</pre>";
/***************
* Try to save this as a file and run the script
************
* If you hover the mouse over the link, they will look exactly the same
* but if you click them, they will output differently
***************/
// Cheers, "Josso"
* This is definitely not what this site is about
* This is not a site where you can get help with various problems
* It's a site where you can say:
* "How do you do the following in your language"
*
*************
*
* Anyway...
* I would use urlencode like you have tried
* (or to be on the safe side: rawurlencode() )
************************/
$parameter = "Bart & Lisa";
echo "<a href='".basename(__FILE__)."?fname=".$parameter."'>?fname=".$parameter."</a><br />\n";
echo "<a href='".basename(__FILE__)."?fname=".rawurlencode($parameter)."'>?fname=".rawurlencode($parameter)."</a><br />\n";
echo "<pre>", print_r($_REQUEST, true), "</pre>";
/***************
* Try to save this as a file and run the script
************
* If you hover the mouse over the link, they will look exactly the same
* but if you click them, they will output differently
***************/
// Cheers, "Josso"
echo("The given input URL is mal-formed; it should be URL-encoded. Use urldecode() on each argument to decode it.");
$url = parse_url("http://myserver.com/custinfo/edit.php?mode=view&fname=Ron & Jean&lname=Smith");
$query = $url['query'];
$query = preg_replace("/&(?=[^=]*&)/", "%26", $query);
parse_str($query, $querystring);
var_dump($querystring);
the result is >> array(3) {
["mode"]=>
string(4) "view"
["fname"]=>
string(10) "Ron & Jean"
["lname"]=>
string(5) "Smith"
}
string-wrap
Wrap the string
Expected output:
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps over t
> he lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox
> jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The qui
> ck brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy
> dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps o
> ver the lazy dog. The quick brown fox jumps over the lazy dog.
"The quick brown fox jumps over the lazy dog. " repeated ten times to a max width of 78 chars, starting each line with "> "
Expected output:
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps over t
> he lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox
> jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The qui
> ck brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy
> dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps o
> ver the lazy dog. The quick brown fox jumps over the lazy dog.
php
foreach(str_split(str_repeat("The quick brown fox jumps over the lazy dog. ", 10), 77) as $line)
printf("> %s\n", trim($line));
printf("> %s\n", trim($line));
Define a string containing special characters
Define the literal string
"\#{'}${"}/"
php
$special = "\\#{'}\${\"}/";
$special = '\#{\'}${"}/';
Define a multiline string
Define the string:
"This
Is
A
Multiline
String"
php
$multiline = <<<ML
This
Is
A
Multiline
String
ML;
This
Is
A
Multiline
String
ML;
$multiline = "This
Is
A
Multiline
String";
Is
A
Multiline
String";
$multiline = "This\nIs\nA\nMultiline\nString";
Define a string containing variables and expressions
Given variables a=3 and b=4 output
"3+4=7"
php
echo "$a+$b=".($a+$b);
printf("%d+%d=%d\n", $a, $b, $a + $b);
Reverse the characters in a string
Given the string
"reverse me", produce the string "em esrever"
php
$reversed = strrev("reverse me");
Reverse the words in a string
Given the string
"This is a end, my only friend!", produce the string "friend! only my end, the is This"
php
$reversed_words = implode(" ", array_reverse(explode(" ", "This is the end, my only friend!")));
Text wrapping
Wrap the string
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog.
"The quick brown fox jumps over the lazy dog. " repeated ten times to a max width of 78 chars, starting each line with "> ", yielding this result:
> The quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog. The quick brown fox jumps
> over the lazy dog. The quick brown fox jumps over the lazy dog. The
> quick brown fox jumps over the lazy dog.
php
$s = str_repeat("The quick brown fox jumps over the lazy dog. ", 10);
$s = wordwrap($s, 76, "WRAP");
$s = explode("WRAP", $s);
foreach ($s as $part) {
$res .= "> " . $part . "\n";
}
echo $res;
$s = wordwrap($s, 76, "WRAP");
$s = explode("WRAP", $s);
foreach ($s as $part) {
$res .= "> " . $part . "\n";
}
echo $res;
echo '> '.wordwrap(str_repeat('The quick brown fox jumps over the lazy dog. ', 10), 70,"\n> ")."\n";
Remove leading and trailing whitespace from a string
Given the string
" hello " return the string "hello".
php
$hello_trimmed = trim(" hello ");
Simple substitution cipher
Take a string and return the ROT13 and ROT47 (Check Wikipedia) version of the string.
For example:
String is: Hello World #123
ROT13 returns: Uryyb Jbeyq #123
ROT47 returns: w6==@ (@C=5 R`ab
For example:
String is: Hello World #123
ROT13 returns: Uryyb Jbeyq #123
ROT47 returns: w6==@ (@C=5 R`ab
php
function rot13($str) {
return strtr($str,
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm",
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz");
}
function str_rot47($str) {
return strtr($str,
'!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
'PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO');
}
echo str_rot13("Hello World #123"); // builtin version
echo str_rot47("Hello World #123"); // homemade version
echo rot13("Hello World #123"); // homemade version
return strtr($str,
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm",
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz");
}
function str_rot47($str) {
return strtr($str,
'!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
'PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO');
}
echo str_rot13("Hello World #123"); // builtin version
echo str_rot47("Hello World #123"); // homemade version
echo rot13("Hello World #123"); // homemade version
Make a string uppercase
Transform
"Space Monkey" into "SPACE MONKEY"
php
echo strtoupper("Space Monkey");
Make a string lowercase
Transform
"Caps ARE overRated" into "caps are overrated"
php
echo strtolower("Caps ARE overRated");
Capitalise the first letter of each word
Transform
"man OF stEEL" into "Man Of Steel"
php
echo ucwords(strtolower("man OF stEEL"));
