doushishi2415 2016-07-17 01:58 采纳率: 0%
浏览 87
已采纳

str_replace()不替换为<strong>元素

I want to be able to create a link as follows, when a user starts typing in a search field. Let's say he types the letter a:

<a href="http://google.com">#<strong>a</strong>rig<strong>a</strong>to</a>

PHP:

// sets up database conection to variable $dbh
require_once '../includes/bootstrap.php';

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $tag = (!empty($_GET['tag'])) ? "%$_GET[tag]%" : false ;

    if ($tag) {
        $stmt = $dbh->prepare('SELECT `tag` FROM `tags` WHERE `tag` LIKE ?');
        $result = array();

        $stmt->bindParam(1, $tag, PDO::PARAM_STR);
        $stmt->execute();

        // store result
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $result[] = $row['tag'];
        }

        $tags = '';

        // create links for results
        foreach ($result as $value) {
            $row = "<li><a href='http://google.com'>" . str_replace($tag, '<strong>' . $tag . '</strong>', $value) . '</a></li>';
            $tags .= $row;
        }
        echo $tags;
    }
}

Result of $tags when user types in the letter a:

<li><a href="http://google.com">#arigato</a></li>
<li><a href="http://google.com">#arizona</a></li>
<li><a href="http://google.com">#cantalupi</a></li>
<li><a href="http://google.com">#clearwater</a></li>
<li><a href="http://google.com">#florida</a></li>
<li><a href="http://google.com">#happy</a></li>
<li><a href="http://google.com">#mamadas</a></li>
<li><a href="http://google.com">#miriam</a></li>
<li><a href="http://google.com">#nissan</a></li>
<li><a href="http://google.com">#sauce</a></li>
<li><a href="http://google.com">#sentra</a></li>
<li><a href="http://google.com">#usa</a></li>
<li><a href="http://google.com">#vegas</a></li>
<li><a href="http://google.com">#was</a></li>
<li><a href="http://google.com">#watches</a></li>

For some reason it is not putting in the <strong> tag as desired.

  • 写回答

1条回答 默认 最新

  • drkj41932 2016-07-17 02:09
    关注

    I think this is happening because of this line:

    $tag = (!empty($_GET['tag'])) ? "%$_GET[tag]%" : false ;

    This variable is used for the MySQL statement, however later on it is also used for the str_replace(), the problem is that it is trying to find %$_GET[tag]% for replacement, not the value in the $_GET variable.

    Try this code instead:

    // sets up database conection to variable $dbh
    require_once '../includes/bootstrap.php';
    
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
        $tagStr = $_GET['tag'];
        $tag = (!empty($_GET['tag'])) ? "%$_GET[tag]%" : false ;
    
        if ($tag) {
            $stmt = $dbh->prepare('SELECT `tag` FROM `tags` WHERE `tag` LIKE ?');
            $result = array();
    
            $stmt->bindParam(1, $tag, PDO::PARAM_STR);
            $stmt->execute();
    
            // store result
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $result[] = $row['tag'];
            }
    
            $tags = '';
    
            // create links for results
            foreach ($result as $value) {
                $row = "<li><a href='http://google.com'>" . str_replace($tagStr, '<strong>' . $tagStr . '</strong>', $value) . '</a></li>';
                $tags .= $row;
            }
            echo $tags;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了