我从数据库中提取以下内容:
总机:265-7404-6,营销热线:265-7403,B'ce:333-6848
我想在PHP中将它转换为这样的东西:
如果扩展名为:265-7404-6
,则仅解析:265-7404
function printdir()
{
$mysqli = new mysqli(constant("host"),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*/
}
}
}
答案 0 :(得分:1)
$telnumber = explode('-', $row['telephone'], 3));
echo $telnumber[0].'-'.$telnumber[1];
扩展名和第二个值之后的任何内容都在$ telnumber [2]
中答案 1 :(得分:0)
<?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;
}
?>
这就是我想要实现的目标。谢谢你们..