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

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;
        }
    }
    
    打赏 评论

相关推荐 更多相似问题