如果字段中不存在“@”符号,如何添加?

How to add '@' symbol if it doesn't exist in the field?

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

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

<?php echo get_the_author_meta('twitter_name'); ?>
<?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;

试试这个……可能会有帮助

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