Robert Rocha Robert Rocha - 4 months ago 24
PHP Question

str_replace() not replacing with <strong> element

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.

Answer

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