duanmu8911 2011-11-24 11:08
浏览 98
已采纳

php - 如何从数据库中查找字符串中的文本?

Let's say i have:

$title = "Rihanna - Man Down";

and a database multiple rows including one with a field called "name" with the "Rihanna" value

now... how can i check if in $title exists "Rihanna" and if it does how to add a link on "Rihanna" like this:

with other words check if one of the expresions from $title exists the database

  • i think this second formulation it is better
<a href="artist?id=Rihanna">Rihanna</a> - Man Down

I want to do something like youtube does at some of it;s music videos to be more clear

  • 写回答

3条回答 默认 最新

  • duandu8202 2011-11-24 11:24
    关注
    $words = explode("-", $title);
    $result = array();
    
    foreach ($words as $word)
    {
        $word = trim($word);
        $res = mysql_query("SELECT name FROM table WHERE name='" . mysql_real_escape_string($word) . "'");
    
        if (mysql_num_rows($res) > 0)
        {
            $result[] = '<a href="artist?id=' . $word . '">' . $word . '</a>';
        }
        else
        {
            $result[] = $word;
        }
    }
    
    $result = implode($result, " - ");
    

    This will however also link to "artist?id=Pink" if a song from another artist is named Pink, but you have the artist Pink in your database.

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

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊