我在变量$actor
示例:
Actor One, Actor Two,Someone
我需要按人分割字符串。我想我会用爆炸来得到这样的东西:
[0] => Actor One
[1] => Actor Two
[2] => Someone
然后我需要按以下格式构建搜索链接:
http://site.com/?s=Actor+One
http://site.com/?s=Actor+Two
http://site.com/?s=Someone
最后像这样回应:
<a href='http://site.com/?s=Actor+One'>Actor One</a>, <a href='http://site.com/?s=Actor+Two'>Actor Two</a>, <a href='http://site.com/?s=Someone'>Someone</a>
我完全迷失在PHP语法中。感谢帮助。
答案 0 :(得分:2)
(这是作业,不是吗?)
反正:
// $actors is an array with the names
$actors = explode(',', $actor);
foreach ($actors as $name) {
$e_name = urlencode($name);
print "<a href=\"http://site.com/?s={$e_name}\">" . htmlentities($name) . "</a>";
}
答案 1 :(得分:0)
<?php
$arr = array('Actor One','Actor Two','Someone');
foreach($arr as $value){
echo '<a href="http://site.com/?s='.rawurlencode($value).'">'.htmlentities($value).'</a>';
}
?>
哎呀网址编码..我的不好!
答案 2 :(得分:0)
您需要做很多步骤:
explode
将字符串转换为数组(正如您所做的那样)rawurlencode
htmlspecialchars
它所以上面的内容可以翻译成这段代码:
$actors = explode(',', $actor);
foreach($actors as $actor) {
printf('<a href=\'http://site.com/?s=%s\'>%s</a>',
rawurlencode($actor),
htmlspecialchars($actor));
}
答案 3 :(得分:0)
只是发布一个安全版本,该版本与数组内容是否已被恶意准备无关:
$cs = "UTF-8";
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", $cs);
/* ... */
foreach($actor as $name)
{
print('<a href="http://site.com/?s=' . htmlentities(urlencode($name), ENT_QUOTES, $cs)
. '">' . htmlentities($name, ENT_QUOTES, $cs) . '</a>');
在您的所有数组都包含各种字符之后,也可能最好包含编码。