【问题标题】:PHP string concatenation problem?PHP字符串连接问题?
【发布时间】:2010-09-14 03:47:19
【问题描述】:
function formatUpdate($tweet,$dt,$picture,$username)
{
    if(is_string($dt)) $dt=strtotime($dt);

    $tweet=htmlspecialchars(stripslashes($tweet));


       $at = "@" . $username;




    return'
    <li>
    <a href="nano.com/' . $username . '"><img class="avatar" src="images/' . $picture . '" width="48" height="48" alt="avatar" /></a>
    <div class="tweetTxt">
    <strong><a href="nano.com/' . $username . '">' . $username . '</a></strong> '. preg_replace('/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?[^\s\"\']+)/i','<a href="$1" rel="nofollow" target="blank">$1</a>',$tweet).'
    <div class="date">'.relativeTime($dt).'</div> <a class ="reply"  href="?replyto=' echo $at;   '">reply</a>
    </div>
    <div class="clear"></div>
    </li>';

}

【问题讨论】:

  • 您问题中的代码格式很差,最后一行本身就是语法错误。我什至不确定 mattbasta 的编辑是否做了任何改进。能贴出完整的代码吗?
  • 这只是代码的一部分,我只是想给你看代码的sn-ps来证明我的问题!!
  • 嗯,你的代码现在看起来的样子,你既没有将字符串分配给变量,也没有回显它。如果您的代码看起来像这样,那就有问题了。
  • “正确的值”是什么意思?你能添加错误消息还是当前结果+预期结果。
  • 您的问题不清楚。显示 sn-p 似乎根本没有说明问题,因为 sn-p 有语法错误。您需要关闭字符串并以分号结尾。您是要回显字符串吗?

标签: php string concatenation echo


【解决方案1】:

螺栓是对的。通常 concat 问题与混淆代码、文字和右引号/双引号有关。尝试使用 heredoc 来清理您的代码块。

例如,我会执行以下操作来避免盯着代码看的眼睛,并让我的头脑免于疯狂地试图找到语法错​​误的位置(仅限伪编码):

$at = "@$username";
$rt = relativeTime($dt);

$out = <<<raw
    <div class="date">$rt</div>
    <a class ="reply" href="?replyto=$at">reply</a>
raw;

看看它看起来有多简单吧?

要了解heredoc,这里有一个阅读参考。

参考:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

【讨论】:

    【解决方案2】:

    要将变量的值附加到字符串,您无需回显该变量。

    你有

    href="?replyto=' echo $at;   '">reply</a>
    

    改成

    href="?replyto='. $at .'">reply</a>
    

    【讨论】:

    • 你说得对,谢谢,但它在页面上显示@username 而不是“回复”链接的问题?它很奇怪。我认为我做错了连接
    猜你喜欢
    • 1970-01-01
    • 2012-01-28
    • 2011-09-20
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多