Posted on August 29, 2008, Filled under PHP,
Bookmark it
Hi,
Here’s a function which is useful when you need to extract some content between two delimiters. For instance you need to extract content using a robot that connects to a page.
<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/web-programming/php/extracting-content-between-two-delimiters.html
*/
function extract_unit($string, $start, $end)
{
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}
Read more from this entry…
Posted on August 29, 2008, Filled under PHP,
Bookmark it
Hello coders,
Here’s a function that validates a username. It can be used when you have a register form and the user should enter a valid username (that shouldn’t contain illegal characters).
<?php
function validate_username($username)
{
$check = eregi_replace('([a-zA-Z0-9_]+)', "", $username);
if(empty($check))
{
return true;
}
else
{
return false;
}
}
$username = 'john_jones';
$validator = validate_username($username);
if($validator)
{
echo 'The username is valid.';
}
else
{
echo 'The username should contain only letters, numbers and underscores.';
}
?>If you use ‘john#_$%jones’ as an username the function will return ‘false’.
If you have any comments / suggestions feel free to post them.
Posted on August 29, 2008, Filled under PHP,
Bookmark it
This function generates random passwords. It’s useful when you have to reset someone’s account or when you create accounts that need a default password.
<?php
/*
Credits: Bit Repository
http://www.bitrepository.com/web-programming/php/generating-a-random-password.html
*/
function generate_random_password($chars = 7, $type = '')
{
$letters_array = range('a','z');
$numbers_array = range(1,9);
srand((float) microtime() * 10000000); // if PHP Version < 4.2.0
if($type == 'letters')
{
$array = $letters_array;
}
else if($type == 'numbers')
{
$array = $numbers_array;
}
else
{
$array = array_merge($letters_array, $numbers_array);
}
$rand_keys = array_rand($array, $chars);
$password = '';
foreach($rand_keys as $key)
{
$password .= $array[$key];
}
return $password;
}
$password = generate_random_password();
// Outputs a 7 characters password (default)
echo $password;
$password = generate_random_password(10);
// Outputs a 10 characters password
echo $password;
?> If you wish to generate a 10 characters password containing only numbers then here's how you can do it:
$new_password = generate_random_password(10,'numbers');
// Outputs a numeric password containing only numbers.
echo $new_password;
NOTE: You can add ‘letters’ for the second parameter if you want to have only letters in the password.
Read the rest of this entry…
Posted on August 28, 2008, Filled under PHP,
Bookmark it
Hi,
This function takes a regular phrase and formats it into a seo friendly string which can be part of an URL.
<?php
function friendly_seo_string($string, $separator = '-')
{
$string = trim($string);
$string = strtolower($string); // convert to lowercase text
// Recommendation URL: http://www.webcheatsheet.com/php/regular_expressions.php
// Only space, letters, numbers and underscore are allowed
$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
/*
"t" (ASCII 9 (0x09)), a tab.
"n" (ASCII 10 (0x0A)), a new line (line feed).
"r" (ASCII 13 (0x0D)), a carriage return.
*/
$string = ereg_replace("[ tnr]+", "-", $string);
$string = str_replace(" ", $separator, $string);
$string = ereg_replace("[ -]+", "-", $string);
return $string;
}
$str = friendly_seo_string('Lorem Ipsum is simply dummy text.');
// Outputs: lorem-ipsum-is-simply-dummy-text
echo $str;
?>This string can be added to a friendly URL like this:
http://www.yourdomain.com/info/lorem-ipsum-is-simply-dummy-text.html
Recommendation URLs:
http://www.webcheatsheet.com/php/regular_expressions.php
http://www.php.net/manual/function.ereg-replace.php
Posted on August 28, 2008, Filled under PHP,
Bookmark it
Greetings,
Let’s suppose you have a mysql database and you need to update only a part of a field’s value, not all.
This can be done using the following query:
UPDATE `table` SET `field` = REPLACE(`field`, "old_string", "new_string");
Here’s how it will look like in a PHP Code:
<?php
$db_host = 'db_host'; // usually localhost
$db_user = 'db_username';
$db_pass = 'db_password';
$db_name = 'db_name';
$db = mysql_connect($db_host, $db_user, $db_pass)
or die ("Unable to connect to Database Server.");
mysql_select_db ($db_name, $db) or die ("Could not select database.");
$table_name = 'lists';
$table_field_name = 'category';
$initial_string = 'man';
$new_string = 'boy';
$query = "UPDATE `".$table_name."`
SET `".$table_field_name."`=REPLACE(`".$table_field_name."`,
'".$initial_string."', '".$new_string."');";
/* NOTE: You can add conditions to this query in case you want to update
a specific field ID like this:
UPDATE `table_name`
SET `field_name` = REPLACE(`field_name` "old_string", "new_string")
where field_id='id_number_here';
*/
$sql_query = mysql_query($query) or die(mysql_error());
if($sql_query)
{
echo 'Table ' .$table_field_name. ' has been successfully updated.';
}
?>
Feel free to post any comments regarding this tutorial.
Posted on August 27, 2008, Filled under PHP,
Bookmark it
Hi,
In this tutorial we will show you how to show alternate colors between (table) rows. They are nice to use in a site’s design.
First let’s define each color:
<?php
// Define colors here
$color_one = '#FFFFFF';
$color_two = '#E6F2F7';
Let’s build our sample array.
$array = array('apple','pine','strawberry',
'pear','banana', 'cranberry','kiwifruit');We will use a variable that will increment with each loop:
$i = 1; // starting value
?>
Let’s output our values in table rows:
<!-- Table with alternate colors -->
<table width="100%" cellspacing="0">
How we will determine which color to show for each row?
While looping trough the array, we will check if the variable $i is an either an odd or an even number. We will determine this by checking if ($i / 2) results in an integer. If it is, then $i is even ($color_one) . Otherwise, it is odd ($color_two).
<?php
foreach($array as $value)
{
/*
Check if ($i / 2) is an integer and determine which color to show.
We will divide the number by 2
*/
$color = (is_int($i / 2)) ? $color_one : $color_two;
?><tr>
<td bgcolor="<?=$color?>"><?=$value?></td>
</tr>
<?php
$i++; // Increment $i
}
?>
</table>
Here’s the complete code:
<?php
// Define colors here
$color_one = '#FFFFFF';
$color_two = '#BBCCED';
$array = array('apple','pine','strawberry',
'pear','banana', 'cranberry','kiwifruit');
$i = 1;
?>
<!-- Table with alternate colors -->
<table width="100%" cellspacing="0">
<?php
foreach($array as $value)
{
/*
Check if ($i / 2) is an integer and determine which color to show.
We will divide the number by 2
*/
$color = (is_int($i / 2)) ? $color_one : $color_two;
?>
<tr>
<td bgcolor="<?=$color?>"><?=$value?></td>
</tr>
<?php
$i++; // Increment $i
}
?>
</table>The output will look like this:

You can use this in any loop (for, foreach, while). Make sure you have a variable that is incrementing with each row so you can determine the alternate color.
Posted on August 27, 2008, Filled under PHP,
Bookmark it
Hello,
In this short tutorial we will learn how to enable the mod_rewrite module in Apache Server.
Step 1
Locate the folder where apache is installed (in localhost is usually in: C:Program FilesApache Software FoundationApache2.2). Find the folder “conf”. You will find in it the file: httpd.conf.
Step 2
Find the line #LoadModule rewrite_module modules/mod_rewrite.so. Uncomment it so it will look like this:
LoadModule rewrite_module modules/mod_rewrite.so
Look for #AddModule mod_rewrite.c and uncomment it as well.
AddModule mod_rewrite.c
In the same file search for:
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
and replace it with:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
You may have many other attributes between the ‘Directory’ tags. Make sure that AllowOverride is set to ‘All’.
Save the new httpd.conf and restart Apache.
Good luck!