PHP Tricks

PHP tips and tricks, PHP tweaks, PHP tricks

How To Take Out Image Name And Format Or Extension From URL OR IMG Tag

Use preg_match OR preg_match_all to solve this problem.Here i have given simple Example for taking name and image format or extension and stored it in array:

Example1: Taking name and image format or extension from

<img src>

Here is the code:

$url=‘<img src="http://www.computereducationworld.com/logo.jpg">’;

preg_match_all(‘/ (src)(\=\’|\=\”)http:\/\/([a-zA-Z0-9\-\_\/\.]+|)\/([a-zA-Z0-9\-\_]+)\.([a-zA-Z]+)(\’|\”)/’,$url,$ImagesArray1,PREG_SET_ORDER);

print_r($ImagesArray1);

Output:

Array (

[0] => Array (

[0] => src="http://www.computereducationworld.com/logo.jpg"

[1] => src

[2] => ="

[3] [...]

PHP - For secure HTTP connection

This trick force for a secure HTTP connection

if (!($HTTPS == “on”)) {

header (”Location: https://$SERVER_NAME$php_SELF”);

exit;

}  

PHP - Redirect to another page

Redirect to another page in PHP..Here is the code:

 <?

header(“Location: http://www.computereducationworld.com/”);

exit(0);

?>  

PHP - Display date and time

Display date and time with PHP:

  <?

/* Today is March 9, 2009, 6:25 am */

$today = date(“F j, Y, g:i a”); // March 9, 2009, 6:25 am

$today = date(“m.d.y”); // 03.09.09

$today = date(“j, n, Y”); // 9, 3, 2009

$today = date(“Ymd”); // 20090309

$today = date(“D M j G:i:s T Y”);// Mon Mar 9 6:25:11 [...]

PHP - Send an alert email

This trick helps you to send an alert mail when there is a new registration on your site or when there is some critical error in your PHP script.

  <?

mail(“recipient@example.com”,

“email subject”,

“Body of the message”,

“From: webmaster@mydomain.com\r\n”);

?>  

PHP - Optimize database table … Easy trick

How to optimize PHP database table… Here code goes:

 dbConnect()

$alltables = mysql_query(”SHOW TABLES”);

 

while ($table = mysql_fetch_assoc($alltables))

{

foreach ($table as $db => $tablename)

{

mysql_query(”OPTIMIZE TABLE ‘”.$tablename.”‘”)

or die(mysql_error());

}

}

PHP - make webpage password protected

This trick helps you protect your web page with a help of a password…Here is the code:

 <?

$username = “someuser”;

$password = “somepassword”;

 

if ($_POST[‘txtUsername’] != $username || $_POST[‘txtPassword’] != $password) {?>

<h1>Login</h1>

<form name=”form” method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>”>

<p><label for=”txtUsername”>Username:</label>

<br><input type=”text” title=”Enter your Username” name=”txtUsername”></p>

 

<p><label for=”txtpassword”>Password:</label>

<br><input type=”password” title=”Enter your password” name=”txtPassword”></p>

<p><input type=”submit” name=”Submit” value=”Login”></p>

</form>

<?} else {?>

<p>This is the [...]