将URL与分隔的关键字相关联

时间:2011-05-20 17:05:04

标签: php url keyword

我有一个变量$keywords

此变量的内容是以逗号或空格分隔的单词 例如:

$keywords= key1,key2,key3

或者

$keywords=key1 key2 key3

我拥有的表格是:

        <table width="500" border="1">
  <tr>
    <td height='auto'>Keywords: $keywords</td>
  </tr>
</table>

我想在key1 key2 key3中爆炸$keywords ... 并将每个分离的单词与预先设定的URL相关联:

http://miosite.com/search/label/key1
http://miosite.com/search/label/key2
http://miosite.com/search/label/key3

所以我想得到这个:

       <table width="500" border="1">
  <tr>
    <td height='auto'>Keywords: key1,key2,key3</td>
  </tr>
</table>

哪里

        key1=http://miosite.com/search/label/key1
        key2=http://miosite.com/search/label/key2
        key1=http://miosite.com/search/label/key3

如何?

4 个答案:

答案 0 :(得分:1)

您可以检查逗号以确定要分解的分隔符,然后再次将字符串放在一起:

if (strpos($keywords,",") !== FALSE) {
  $keys = explode(",",$keywords);
} else {
  $keys = explode(" ",$keywords);
}
$keywords = "";
foreach ($keys as $key) $keywords .= "http://miosite.com/search/" . $key . "<BR>";

编辑:显然,对象是删除网站地址,而不是添加它...没有人想到这一点。新代码:

if (strpos($keywords,",") !== FALSE) {
  $keys = explode(",",$keywords);
} else {
  $keys = explode(" ",$keywords);
}
$keywords = "";
foreach ($keys as $key) $keywords .= str_ireplace("http://miosite.com/search/","",$key) . ",";
$keywords = substr($keywords,0,strlen($keywords)-1);

哈金,但是试一试。

编辑:哦,现在他们需要联系? LOL

if (strpos($keywords,",") !== FALSE) {
  $keys = explode(",",$keywords);
} else {
  $keys = explode(" ",$keywords);
}
$keywords = "";
foreach ($keys as $key) {
   $newkey = str_ireplace("http://miosite.com/search/","",$key);
   $keywords .= "<a href=\"" . $key . "\">" . $newkey . "</a>,";
}
$keywords = substr($keywords,0,strlen($keywords)-1);

好的,试试吧。

答案 1 :(得分:0)

$keywords = explode(',',$keywords);
foreach($keywords as $key) echo("<a href=\"http://miosite.com/search/{$key}\">{$key}</a>");

答案 2 :(得分:0)

       关键词:

$keywords = explode(',',$keywords);
$tmpArray = array();
foreach($keywords as $key) {
    $tmpArray[] = 'http://miosite.com/search/label/'.$key;
}

echo implode(",", $tmpArray);

答案 3 :(得分:0)

我只是通过embeddg php在html&amp; amp;你的链接之间是caomma:

 <?php
$keywords = 'key1 key2 key3';
$keywords = preg_split('/[, ]/',$keywords);

foreach($keywords as $key)
    $links[]= "<a href=http://miosite.com/search/{$key}\>{$key}</a>";
    print_r($links);

$keywords  = implode(',',$links);   

?>

<table width="500" border="1">
  <tr>
    <td height='auto'>Keywords: <?php  echo $keywords ;?>
</td>
  </tr>
</table>