douhu2370 2011-05-20 17:05
浏览 27
已采纳

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

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?

  • 写回答

4条回答 默认 最新

  • dounan9070 2011-05-20 17:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用