Removing white spaces and other unwanted characters.

#> remove excess spaces to the front and end of string.
#> 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,

$mystring = "This string says, "I am a string", so there.";

A way to get around not escaping quotes in strings is,

$mystring = <<<eof
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.

$variable = "Variable1";

Set the dynamic variable.

$$variable = "Contents of Variable1"; // i.e. $Variable1 = "Contents of Variable1";
print("$Variable1"); // outputs: Contents of Variable1

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)

<?xml version="1.0" ?>
<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

$a = simplexml_load_file('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

<?php
$firstname = $_SERVER['argv'][1];
$lastname = $_SERVER['argv'][2];

print($firstname . ' ' . $lastname . "n");
?>

run this php file with

$ php file.php myfirstname mylastname

this outputs

myfirstname mylastname

MySQL

$query = sprintf("select field from table where column='%s'", mysql_real_escape_string($value));
$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.

$myauthor = mysql_query("select auid,artid,firstName,MI,lastName from author where artid=$artid",$myid);
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

$myid = mysql_pconnect('host','username','password');

Selecting a database

mysql_select_db('database',$myid);

Select one row and output results

$myresult = mysql_fetch_row(mysql_query("select field1, field2 from table where id = 1",$myid));
$field1 = $myresult[0];
$field2 = $myresult[1];
print("field1 = $field1 and field2 = $field2");

Select multiple rows

$myresult = mysql_query("select field1, field2 from table where id >= 1 and id <= 10",$myid);
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,

<?php
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.

<html>
<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.

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.