简体   繁体   中英

How to add <tag> to php string

I'm kinda new to PHP and I'm trying to add a tag within a PHP variable.

This is what I have, and it's not working.

$linkText .= "<span>" . ( 'Reply', $this->_slug ) . "</span>";

Can anyone please point me in the right direction?

Thanks :)

UPDATE:

This is the complete working code:

$linkText = __( 'Reply', $this->_slug );
$linkAttrs['href'] = "http://twitter.com/intent/tweet?in_reply_to={$tweet->id_str}";
$linkAttrs['class'] = 'in-reply-to';
$linkAttrs['title'] = $linkText;
$widgetContent .= $this->_buildLink( $linkText, $linkAttrs );

What I'm trying to do here is to add a tag around $linkText so that I get a result like

<a href="#"><span>Title</span></a>
$linkText .= '<span>Reply ' . $this->_slug . '</span>';

There's no need for brackets, you can basically concatenate (join) strings and variables using . . I have merged the 'Reply' into the span tag as they are both strings, only the PHP variable $this->_slug and and the strings need separating with .

Here is the manual page for strings in PHP .

Based on your update, and checking out the twitter plugin source code , here is the code I think you need:

$linkText = '<span>' . __( 'Reply', $this->_slug ) . '</span>';
$linkAttrs['href'] = "http://twitter.com/intent/tweet?in_reply_to={$tweet->id_str}";
$linkAttrs['class'] = 'in-reply-to';
$linkAttrs['title'] = $linkText;
$widgetContent .= $this->_buildLink( $linkText, $linkAttrs, true );

I've also passed a true parameter to the buildLink() which prevents the <span> tag from being escaped.

You're trying to concatenate parenthesis without putting it between a pair of quotes and you should use dots instead of commas.

$linkText .= "<span>(Reply " . $this->_slug . ")</span>";

PS Remove the paranthesises if you think I got you wrong and you don't need them :)

我不太确定我是否正确,但是如果$ this-> _ slug是一个函数,则必须这样调用它:

$linkText .= "<span>" . $this->_slug('Reply') . "</span>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM