Removing white spaces and other unwanted characters.
#> trim() also accepts a second parameter trim($str,"aeiou");
#> The previous example with ''aeiou'' will only strip those letters
#> from beginning and end of a string. Without the second parameter
#> even \\n and \\r and \\t ... etc will be removed.
$str = trim($str);
#> convert multiple spaces into one. "Hello World ---> Hello World"
#> this just continuously looks for a pair of spaces and moves them to one.
while(strpos($str," ")){
$str = str_replace(" "," ",$str);
}
Strings with quotations
The standard way to store a string that contains quotations would be,
A way to get around not escaping quotes in strings is,
This string says, "I am a string", so there.
eof;
eof is just an example - you can write anything. It just signafies the start and stop of the string(heredoc).
Note: when printing an array inside of a heredoc you must enclose it in braces. e.g. {$myarray[0]}. Also, sometimes the first { in front of a $ should be escaped as {$
Variable Variables
Give $variable a value.
Set the dynamic variable.
XML
If you'd like to use simplexml functionality in PHP4, you can use a script that mimics it from http://www.criticaldevelopment.net/xml/doc.php ».
Read and print information from an XML file (PHP 5)
<a name="a1">
<b name="b1">b1 content</b>
<b name="b2">
<c name="c1">c2 content</c>
</b>
</a>
Save the above xml as sample.xml
print($a['name']); // outputs: a1
print($a->b[0]); // outputs: b1 content
$b1 = $a->b[0];
print($b1[name]); // outputs: b1
print($a->b[1]->c); // outputs: c2 content
foreach($a->b as $b){
print($b['name']); // outputs: b1 b2
}
Note, since has only one node, you do not have to refer to it as an array. That's why you say $a->b[0] instead of $a[0]->b[0]. Also, note that you use $a instead of just 'a', like for b and c. $a is just the value you read the xml into with the simplexml_load_file command.'''
Scripts at the command line
When you execute a php script from the command line with
you can pass arguments to the file by adding $_SERVER['argv'][1] to the php file. The 1 stands for the argument number. Below is an example of how this works.
php file file.php
$firstname = $_SERVER['argv'][1];
$lastname = $_SERVER['argv'][2];
print($firstname . ' ' . $lastname . "n");
?>
run this php file with
this outputs
MySQL
$result = mysql_query($query,$myid);
Trigger a file download
header("Content-type: applications/x-download");
header("Content-Length: " . filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
readfile($filename);
?>
Grab field name and contents and print from MySQL.
print("<table><tr>");
while($author = mysql_fetch_row($myauthor)){
print("<td>");
for($i=0;$i<count($author);$i++){
print(mysql_field_name($myauthor,$i) . ": " . $author[$i] . "<br />");
}
print("</td>");
}
print("</tr></table>");
print("<hr />");
Making a connection
Selecting a database
Select one row and output results
$field1 = $myresult[0];
$field2 = $myresult[1];
print("field1 = $field1 and field2 = $field2");
Select multiple rows
while($result = mysql_fetch_row($myresult)){
$field1 = $result[0]; $field2 = $result[1];
print("field1 = $field1 and field2 = $field2 <br />");
}
Hiding locations of images but still displaying them.
If you have a picture pic.jpg located in /usr/local/pics you can hide the location of the picture by doing something like this.
Create a script say, render.php and in it put,
if($file != '' && $dir != ''){
$dir = base64_decode($dir);
readfile($dir . '/' . $file);
die();
}
?>
When you call the picture pic.jpg in your html or php script, do it like this.
<head><title>render picture</title></head>
<body>
<?php
$file = "pic.jpg";
$dir = base64_encode("/usr/local/pics");
print <<<eof
<img src="/render.php?file={$file}&dir={$dir}" alt="{$file}" />
eof;
?>
</body>
</html>
This should encode and hide the location of your pictures, at least as long as someone doesn't figure out to do a base64_decode() on your $dir.
Magic Quotes and proper mysql insert and query
If in your php.ini file you have magic quotes on, then php will auto escape quotes. If not, you will have to use something like mysql_escape_string.
[Click to add or edit comments])
Please prepend comments below including a date