MYSQL数据库电话号码表记录处理代码片段
自己给朋友做的一个处理,临时用的一个代码片段,记录一下,以备不时之需。其中用到了几个正则表达式和部分foreach循环。
<?php
ignore_user_abort (true);
set_time_limit (0);
class mysql
{
public $fetch_mode = MYSQL_BOTH;
public $record = array( );
public $count = NULL;
public function __construct( )
{
$this->connect( );
}
public function connect( )
{
if ( !( $this->link = mysql_connect( "localhost", "root", "" ) ) )
{
exit( mysql_error( ) );
}
mysql_query( "SET NAMES UTF8" );
if ( !mysql_select_db( "two", $this->link ) )
{
exit( "未能找到数据库:" );
}
}
public function ping( )
{
if ( !mysql_ping( $this->link ) )
{
mysql_close( $this->link );
$this->connect( );
}
}
public function query( $sql )
{
mysql_query( "SET NAMES UTF8" );
return mysql_query( $sql );
}
public function num_rows( $result )
{
return mysql_num_rows( $result );
}
public function affected_rows( )
{
return mysql_affected_rows( );
}
public function num_fields( $result )
{
return mysql_num_fields( $result );
}
public function free_result( $result )
{
return mysql_free_result( $result );
}
public function insert_id( )
{
return mysql_insert_id( );
}
public function close( )
{
return mysql_close( );
}
public function fetch_array( $result )
{
if ( $result )
{
return mysql_fetch_array( $result, $this->fetch_mode );
}
}
public function fetch_row( $rs )
{
$this->record = mysql_fetch_array( $rs, $this->fetch_mode );
return $this->record;
}
public function fetch_all( $rs )
{
$arr = array( );
while ( $this->record = mysql_fetch_array( $rs, $this->fetch_mode ) )
{
$arr[] = $this->record;
}
return $arr;
}
}
$db = new mysql();
$sql = "select * from reg where 1";
if ( !( $rs = $db->query( $sql ) ) )
{
exit( mysql_error( ) );
}
while ( $row = $db->fetch_array( $rs ) )
{
$arr = array();$i =0;
$row['email']=trim($row['email']);
$b=explode(" ",$row['email']);
foreach($b as $key){
if(strlen($key)==11){
$regex = "/13\d{9}|15\d{9}|18\d{9}|14\d{9}/";
preg_match_all($regex,$key, $phones);
if($phones[0][0]){$arr[]= $key;}else{$i++;}
}else{ $i++;}
}if($i > 0){continue;}
$row['content'] = trim($row['content']);
foreach ($arr as $key)
{
$sqls = "insert into phone (name,phone,content) values ('".$row['name']."','".$key."','".$row['content']."')";
if (!( $rss = $db->query( $sqls ) ) )
{
exit( mysql_error( ) );
}
}
$sqlss = "delete from reg where id = '".$row['id']."'";
if ( !( $rssss = $db->query( $sqlss ) ) ){exit( mysql_error( ) );}
// $row['email']=trim($row['email']);
//
// if(strlen($row['email'])==12)
// {
// $regex = "/021-[0-9]{8}/";
// preg_match_all($regex,$row['email'], $phones);
// if($phones[0][0])
// {
// $row['email'] = str_replace("021-","",$phones[0][0]);
// $row['content'] = trim($row['content']);
// $sqls = "insert into phone (name,phone,content) values ('".$row['name']."','".$row['email']."','".$row['content']."')";
// if ( $rss = $db->query( $sqls ) )
// {
// $sqlss = "delete from reg where id = '".$row['id']."'";
// if ( !( $rssss = $db->query( $sqlss ) ) ){exit( mysql_error( ) );}
// }
//
// }
// }
//
// $row['email']=trim($row['email']);
//
// if(strlen($row['email'])==10)
// {
// $regex = "/800[0-9]{7}/";
// preg_match_all($regex,$row['email'], $phones);
// if($phones[0][0])
// {
// $row['email'] = str_replace("-","",$phones[0][0]);
// $row['content'] = trim($row['content']);
// $sqls = "insert into phone (name,phone,content) values ('".$row['name']."','".$row['email']."','".$row['content']."')";
// if ( $rss = $db->query( $sqls ) )
// {
// $sqlss = "delete from reg where id = '".$row['id']."'";
// if ( !( $rssss = $db->query( $sqlss ) ) ){exit( mysql_error( ) );}
// }
//
// }
// }
}
?>
雨田博客