I have the following being extracted from a database:
Switchboard:265-7404-6,Marketing Hotline: 265-7403, B'ce: 333-6848
I want to convert it to something like this in PHP:
In the event of extension as: 265-7404-6
it only parses: 265-7404
function printdir()
{
$mysqli = new mysqli(constant("h开发者_如何转开发ost"),constant("username"),constant("password"), constant("database"));
$storedProc = "SELECT * FROM `directory`;";
$result = $mysqli->query($storedProc);
if($mysqli->affected_rows>0)
{
while( $row = $result->fetch_assoc()) {
echo $row['telephone']; /*Formatting here*/
}
}
}
$telnumber = explode('-', $row['telephone'], 3));
echo $telnumber[0].'-'.$telnumber[1];
The extension and anything after the second value will be in $telnumber[2]
<?php
function format_number($phone){
$phone = preg_replace("#[^\d{10}\s]#",'',$phone);
if(strlen($phone)==7)
{
$area = substr($phone,0,3);
$prefix = substr($phone,3,4);
$format = $area."-".$prefix;
$phone = "<a href=\"tel:".$format."\">$format</a>";
return($phone);
}
if(strlen($phone) != 10) return(False);
$area = substr($phone,0,3);
$prefix = substr($phone,3,3);
$number = substr($phone,6,4);
$format = $area."-".$prefix."-".$number;
$phone = "<a href=\"tel:".$format."\">$format</a>";
return($phone);
}
function telephoneText($string)
{
$format_one.= '\d{10}'; //5085551234
$format_two_and_three .= '\d{3}(\.|\-)\d{3}\2\d{4}'; //508.555.1234 or 508-555-1234
$format_four .= '\(\d{3}\)\-\d{3}\-\d{4}'; //(508)-555-1234
$format_five .='\d{3}\-\d{4}'; //(508)555-1234
$format_five_seven .='\d{7}';
$string = preg_replace("~({$format_one}|{$format_two_and_three}|{$format_four}|{$format_five}|{$format_five_seven})~e", 'format_number("$1")', $string);
return $string;
}
?>
Thats is the what i was trying to achieve. Thanks guys..
精彩评论