Php get_meta_tags-Extracts all meta tag content attributes
PMA00:10
get_meta_tags — Extracts all meta tag content attributes from a file and returns an array
<?php// Assuming the above tags are at www.example.com$tags = get_meta_tags('http://www.example.com/');
// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.echo $tags['author']; // nameecho $tags['keywords']; // php documentationecho $tags['description']; // a php manualecho $tags['geo_position']; // 49.33;-86.59?>
PHP Redirect - Redirect Script?
PMA00:07
PHP Redirect Function
header('Location: destination.php'); exit();
ou need the Location:
part so the browser knows what header it's
receiving. Also, don't forget to
do anexit()
ordie()
right after the redirect.
Finding the Position of a Value in an Array
PMA05:54
Use array_search( ) . It
returns the key of the found value. If the value is not in the array, it returns
false:
$position = array_search($value, $array); if ($position !== false) { // the element in position
//$position has $value as its
//value in array $array }
Use in_array( ) to find if an
array contains a value; use array_search( ) to discover where that value
is located. However, because array_search( ) gracefully handles searches in which the value isn't
found, it's better to use array_search( ) instead of in_array( ).
The speed difference is minute, and the extra information is potentially
useful:
$food = 'cauliflower'; $position = array_search($food, $favorite_foods);
if ($position !== false)
{
echo "My #$position favorite food is $food";
}
else {
echo "Blech! I hate $food!";
}
Use the !== check against false because if your
string is found in the array at position 0, the if evaluates
to a logical false, which isn't what is meant or wanted.
If a value is in the array multiple times, array_search(
) is only guaranteed to return one of the instances, not the first
instance.
Turning an Array into a String
PMA05:52
convert it into a formatted string.
// make a comma delimited list $string = join(',', $array);
Or loop yourself:
$string = ''; foreach ($array as $key => $value) { $string .= ",$value"; } $string = substr($string, 1); // remove leading ","
If you can use join( ), do; it's faster than any
PHP-based loop. However, join( ) isn't very flexible. First, it places a
delimiter only between elements, not around them. To wrap elements inside HTML
bold tags and separate them with commas, do this:
$left = '<b>';
$right = '</b>';
$html = $left . join("$right,$left", $html) . $right;
Second, join( ) doesn't allow you to discriminate
against values. If you want to include a subset of entries, you need to loop
yourself:
$string = '';
foreach ($fields as $key => $value) {
// don't include password
if ('password' != $key) {
$string .= ",<b>$value</b>";
}
}
$string = substr($string, 1); // remove leading ","Setting Environment Variables
PMA05:50
Setting environment variables in your server configuration
on a host-by-host basis allows you to configure virtual hosts differently.
on a host-by-host basis allows you to configure virtual hosts differently.
<?php putenv('ORACLE_SID=ORACLE'); // configure oci extension ?>
Adjusting behavior based on an environment variable
<?php $version = $_SERVER['SITE_VERSION']; // redirect to http://guest.example.com,
//if user fails to sign in correctly if ('members' == $version) { if (!authenticate_user($_POST['username'], $_POST['password'])) { header('Location: http://guest.example.com/'); exit; } } include_once "${version}_header"; // load custom header
http://www.php.net/putenv; information on setting environment variables in Apache at
Extracting Substrings
PMA05:47
You want to
extract part of a string, starting at a particular place in the string. For
example, you want the first eight characters of a username entered into a
form.
Extracting a substring with substr(
)
<?php $substring = substr($string,$start,$length); $username = substr($_GET['username'],0,8); ?>
If $start and $length are positive, substr( ) returns $length characters in the string, starting at $start.
If $start is bigger than the length of the string,
substr( ) returns false..
Setting Default Values for Function Parameters
PMA05:45
Assign the default value to the parameters inside the function
prototype:
function wrap_html_tag($string, $tag = 'b') { return "<$tag>$string</$tag>"; }
The example in the Solution sets the default tag value to
b, for bold. For example:
$string = 'I am some HTML';
wrap_html_tag($string);
returns:
<b>I am some HTML</b>
This example:
wrap_html_tag($string, 'i');
returns:
<i>I am some HTML</i>
There are two important things to remember when assigning
default values. First, all parameters with default values must appear after
parameters without defaults. Otherwise, PHP can't tell which parameters are
omitted and should take the default value and which arguments are overriding the
default. So wrap_html_tag( ) can't be defined
as:
function wrap_html_tag($tag = 'i', $string)
If you do this and pass wrap_html_tag( ) only a single
argument, PHP assigns the value to $tag and issues a warning
complaining of a missing second argument.
Second, the assigned value must be a constant, such as a string
or a number. It can't be a variable. Again,
using wrap_html_tag( ), such
as our example, you can't do this:
$my_favorite_html_tag = 'i';
function wrap_html_tag($string, $tag = $my_favorite_html_tag) {
...
}
If you want to assign a default of nothing, one solution is to
assign the empty string to your parameter:
function wrap_html_tag($string, $tag = '') {
if (empty($tag)) return $string;
return "<$tag>$string</$tag>";
}
This function returns the original string, if no value is
passed in for the $tag. Or if a (nonempty) tag is passed in, it returns
the string wrapped inside of tags.
Depending on circumstances, another option for the
$tag default value is either 0 or NULL. In
wrap_html_tag( ), you don't want to allow an empty-valued tag. However,
in some cases, the empty string can be an acceptable option. For instance, join( ) is often called on the empty string,
after calling file( ), to place a file into a
string. Also, as the following code shows, you can use a default message if no
argument is provided but an empty message if the empty string is passed:
function pc_log_db_error($message = NULL) {
if (is_null($message)) {
$message = 'Couldn't connect to DB';
}
error_log("[DB] [$message]");
}CuteNews right
PMA02:53
Additional X-Field, ability to see the first posts while adding and
editing,
PHP-code error protection while news output, error logging at backend.
A notable feature of the CuteNews engine is that it doesn't use MySQL to store news,
comments, user profiles, or any other data.
The CuteNews engine can be installed on practically any web server.
CuteNews allows for different types of users with different permission levels: Administrator, Editor, Journalist, Commenter. Each of these types of users has access to different features on a
CuteNews-driven website.
You don't have to worry about creating news items: CuteNews does everything automatically. It is also capable of archiving old news items and activating postponed ones on its own.
Download Cute News 1.5.2 zip (570 Kb)
PHP-code error protection while news output, error logging at backend.
A notable feature of the CuteNews engine is that it doesn't use MySQL to store news,
comments, user profiles, or any other data.
The CuteNews engine can be installed on practically any web server.
CuteNews allows for different types of users with different permission levels: Administrator, Editor, Journalist, Commenter. Each of these types of users has access to different features on a
CuteNews-driven website.
You don't have to worry about creating news items: CuteNews does everything automatically. It is also capable of archiving old news items and activating postponed ones on its own.
Download Cute News 1.5.2 zip (570 Kb)