如何添加' @'符号,如果它不存在于该领域?

时间:2015-02-22 18:34:42

标签: php wordpress

我在Wordpress中输出自定义用户字段,这是用户的Twitter用户名。有些用户可能会添加' @'符号,有些可能没有。如何输出该字段并检查符号是否已存在,如果不存在,请添加它吗?

这是我用来输出usrename的代码:

<?php echo get_the_author_meta('twitter_name'); ?>

2 个答案:

答案 0 :(得分:2)

<?php
$authorMeta = get_the_author_meta('twitter_name');
if (strpos('@', $authorMeta) !== 0) {
    $authorMeta = '@'.$authorMeta;
}
echo $authorMeta;

您需要检查@是否位于第一位,您可以通过多种方式执行此操作

<?php
$authorMeta = get_the_author_meta('twitter_name');
if ($authoMeta[0] != '@') {
    $authorMeta = '@'.$authorMeta;
}
echo $authorMeta;

答案 1 :(得分:1)

试试这个......这可能有帮助

$a = get_the_author_meta('twitter_name');
$b = explode('@',$a);
if($b[1] == 0){
    echo "@".$a;
}
else{
echo $a;
}