我的网站存在问题(使用 PHP 脚本构建), 我想总结全名学生ex:" Ahmad Najib Fuadi" 从数据库到短名称" A.N。我的网站上的Fuadi" :
我已经拥有这样的数据库:
| FNAME | LNAME |
|--------------|----------|
| Ahmad Najib | Fuadi |
我尝试过这样的代码:
<tbody>
<?php if($query):foreach($query as $post):?>
<tr>
<td class="center"><?php echo $post->FNAME; ?></td>
<td class="center"><?php echo $post->LNAME ?></td>
</tr>
<?php endforeach; ?>
<?php endif;?>
</tbody>
我不知道缩短名字(FNAME)
由于
答案 0 :(得分:2)
我修改了代码检查并确认
<?php if($query):foreach($query as $post):?>
<tr>
<td class="center">
<?php
$fname = $post->FNAME;
$firstName = getAbrName($fname); /// call function with pass first name
//$firstName = substr($fname,0,1).'.';
//if(strpos($fname,' ')){
// $firstName .= substr($fname,(strpos($fname,' ')+1),1).'.';
//}
echo $firstName;
?>
</td>
<td class="center"><?php echo $post->LNAME;?></td>
</tr>
<?php endforeach; ?>
<?php endif;?>
<?php function getAbrName($name = ''){
$nameArray = explode(' ',$name);
$returnName = '';
if(is_array($nameArray)){
foreach($nameArray as $value){
$returnName .= substr($value,0,1).'.';
}
}
return $returnName;
}
?>
答案 1 :(得分:2)
尝试我的功能getShortName
function getShortName($fname, $lname){
$name = '';
foreach(explode(' ', $fname) as $fn)
$name .= strtoupper($fn[0]) . '. ';
$name .= ucfirst($lname);
return $name;
}
测试数据:
$names = array(
array('fname' => 'Ahmad', 'lname' => 'Fuadi'),
array('fname' => 'Ahmad Najib', 'lname' => 'Fuadi'),
array('fname' => 'Ahmad najib mama', 'lname' => 'Fuadi'),
array('fname' => 'ahmad baba Mama', 'lname' => 'Fuadi'),
array('fname' => 'allen iverson', 'lname' => 'chak')
);
foreach($names as $n)
print(getShortName($n['fname'], $n['lname']) . "<br />");
测试结果:
A. Fuadi
A. N. Fuadi
A. N. M. Fuadi
A. B. M. Fuadi
A. I. Chak
答案 2 :(得分:0)
请尝试以下代码:
$first = "Ahmad Najib";//fetch real first name from the database
$last = "Fuadi";//fetch real name from the database
$result = rtrim(preg_replace('#([^ ])[^ ]* *#i', '$1. ', $first)," ");
$result = "$result $last";
echo "$result";//or do something else with the shorter name
该方法非常健壮,例如双空格(" "
)被解释为一个空格等。
您可以测试此代码here。
因此,当将此方法与您自己的代码合并时,可以得到:
<tbody>
<?php if($query):foreach($query as $post):?>
<tr>
<td class="center"><?php
$first = $post->FNAME;
$result = rtrim(preg_replace('#([^ ])[^ ]*[ ]*#i', '$1. ', $first)," ");
echo $result;
?></td>
<td class="center"><?php echo $post->LNAME ?></td>
</tr>
<?php endforeach; ?>
<?php endif;?>
答案 3 :(得分:0)
如下所示。
无论FNAME由多少部分组成,这只会考虑左边前两部分的最大值。
SELECT
CONCAT
(
SUBSTR( FNAME ,1 ,1 ) ,'.'
,IF
(
LOCATE( ' ' ,FNAME ) > 0
,CONCAT( SUBSTR( FNAME FROM ( 1 + LOCATE( ' ' ,FNAME ) ) FOR 1 ) ,'.' )
,''
)
,' '
,LNAME
) AS short_name
FROM table_name
所以:
Ahmad
,Fuadi
结果:A. Fuadi
Ahmad Najib
,Fuadi
结果:A.N. Fuadi
Ahmad Najib Mahmood
,Fuadi
也会产生结果:A.N. Fuadi
答案 4 :(得分:0)
为什么不使用正则表达式?
<tbody>
<?php if($query): foreach($query as $post): ?>
<tr>
<td class="center"><?php echo preg_replace('/(.)[^\s]+\s?/', '${1}.', strtoupper($post->FNAME)); ?></td>
<td class="center"><?php echo $post->LNAME; ?></td>
</tr>
<?php endforeach; endif; ?>
</tbody>