将 URL 与分隔的关键字关联起来

发布于 2024-11-08 13:31:01 字数 984 浏览 2 评论 0原文

我有一个变量 $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

如何?

I have a variable $keywords

The content of this variable are words separated by commas or spaces
for example:

$keywords= key1,key2,key3

Or

$keywords=key1 key2 key3

the table that I have is:

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

I want explode $keywords in key1 key2 key3 ...
And associate to each separated word a predifined URL:

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

So I want get this:

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

Where

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

How to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二货你真萌 2024-11-15 13:31:01

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

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);

有点骇人听闻,但请尝试一下。

编辑:哦,现在他们需要链接吗?哈哈

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);

好的,试试吧。

You could check for the comma to determine which delimiter to explode on, then put together the string again:

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

EDIT: Apparently the object is to REMOVE the site address, not add it... no one figured that out. new code:

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);

Hackish, but give that a shot.

Edit: Oh now they need to be linked? 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);

Ok, try that out.

煮茶煮酒煮时光 2024-11-15 13:31:01
$keywords = explode(',',$keywords);
foreach($keywords as $key) echo("<a href=\"http://miosite.com/search/{$key}\">{$key}</a>");
$keywords = explode(',',$keywords);
foreach($keywords as $key) echo("<a href=\"http://miosite.com/search/{$key}\">{$key}</a>");
阳光的暖冬 2024-11-15 13:31:01

关键词:

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

echo implode(",", $tmpArray);

Keywords:

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

echo implode(",", $tmpArray);
勿忘初心 2024-11-15 13:31:01

我只是在 html 和 html 中通过 embeddg php 重写。采取的是您的链接之间的逗号:

 <?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>

Iam just rewrting by embeddg php within html & taken are of caommas between your links:

 <?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文