我正在尝试自动将每个单词或逗号分隔的单词添加到现有 url 中。
我有网址让我们说http://stackoverflow.com/search?q=HERE IS THAT TEXT
.
我有这个功能:
function movie_cast( $atts, $content = null ) {
return '<div class="movie_cast">Cast: '.$content.'</div>';
}
add_shortcode( 'movie_cast', 'movie_cast' );
我正在使用它:[movie_cast]Actor 1, Actor 2[/movie_cast]
由此输出的只是文本:Actor 1, Actor 2
我怎么能这样得到它:<a href="http://stackoverflow.com/search?q=Actor 1">Actor 1</a>, <a href="http://stackoverflow.com/search?q=Actor 2">Actor 2</a>
这是你的意思吗?此代码将像 movie_cast("Actor 1, Actor 2")
或 [movie_cast]Actor 1, Actor 2[/movie_cast]
一样调用,并将返回您请求的输出。
Explode 在逗号上拆分字符串,条件在每个链接后放置一个逗号,除了最后一个链接,其余的只是字符串连接。
function movie_cast( $atts ) {
$url = "http://stackoverflow.com/search?q=";
$str = "";
foreach (explode(", ",$atts) as $value)
{
if ($str != "") $str .= ", ";
$str .= "<a href='"" . $url . $value . "'">" . $value . "</a>";
}
return $str;
}