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



我正在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;
}

最新更新