Wednesday, October 10, 2012

Extract Email ID from Content PHP


In many, CMS development, We may often to check whether any email was found inside the content in this case. Here is the best solution to extract email ids from content using the power of regular expression.

Regular Expression to extract Email IDs from Content.

'/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/'

PHP function to extract All Regular Expression Matches

preg_match_all('/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/',$content,$output);

Sample Content.txt with email id mixed html

<p>example for extracting email id <b>example@gmail.com</b> 
from entire content it mean how many <b>email@example.com</b> 
<b>admin.connnect@example.com</b></p
<?php

//Extract Email ID from content.txt

$whois = file_get_contents("content.txt");
preg_match_all('/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/',$whois,$emails);


echo "<pre>";
print_r($emails[0]);

?>

Output

// OUTPUT


Array
(
    [0] => example@gmail.com
    [1] => email@example.com
    [2] => admin.connnect@example.com
)



Tuesday, October 9, 2012

Optimize All MySQL Tables PHP


If you looking for optimizing MySQL database tables from PHP dynamically, here is a solution to optimize entire tables a database.

Optimizing Tables we need to get Entire table list using "SHOW TABLES" query. as this return entire table names as record, after getting executed of this command call OPTIMIZE TABLE and REPAIR TABLE as per the table needs.

Primary Optimization will failed if the table been damage or corrupt. we will apply REPAIR TABLE

<?php

mysql_connect("localhost","root","");
mysql_select_db("your_db_name");


echo "<br /> OPTIMIZING TABLES ";

$alltables = mysql_query("SHOW TABLES");

// Process all tables.
while ($table = mysql_fetch_assoc($alltables))
{
 foreach ($table as $db => $tablename)
 {
 // Optimize them!
 if(mysql_query("OPTIMIZE TABLE ".$tablename))
  echo "<br>OK Optimized : ".$tablename;
 else
 {
  echo "<br>Error Optimizing Applying Reapir... ".$tablename;
  // Apply Reapirt if Optimization Failed;
  if(mysql_query("REPAIR TABLE ".$tablename))
  echo "Repaired !";
  else
  echo "Error Repairing Table!";
 }
 }
}
mysql_close();
?>