str_replace not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    str_replace not working

    I have this code below and its not working, the $title out puts the title fine but its not wrapping the $q (query word) with a <strong></strong>!???
    Code:
    $sql = mysql_query("SELECT id,title,body,date,time FROM ".$table." WHERE title LIKE '%".$q."%' ORDER BY date DESC, time DESC");
      while($row = mysql_fetch_array($sql))
      {
    	$link_name = preg_replace('#[^a-zA-Z0-9]+#','-',$row['title']);
    	$title = str_replace($q,'<strong>'.$q.'</strong>',$row['title']);
        echo '<div class="search_results_wrapper">
    		<div class="search_results_link_name"><div class="search_results"><a href="'.$link.$row['id'].'-'.$link_name.'.htm">'.$title.'</a></div></div>
    		<div class="search_datetime">'.strftime("%b %d, %Y", strtotime($row['date'])).' at '.wordwrap(substr($row['time'],0,5)).'</div>
    		<div class="search_results_body">'.add_dots(format_article_2($row['body']),'|',150).'</div>
    		<div class="search_results_url">www.ign.ie/'.$link.$row['id'].'-'.$link_name.'.htm</div>
        </div>
    ';
      }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    where should this wrapping happen? I can't spot the place in your code.

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Line 5:
      Code:
      $title = str_replace($q,'<strong>'.$q.'</strong>',$row['title']);

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        You don't define anywhere $q.

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          Its being passed in as a parameter in the function that the code snippet above is from.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            Just for interest sake to see what the problem is, can you try:
            Code:
            $x = "<strong>" . $q . "</strong>";
            echo($x);
            $title = str_replace($q , $x , $row['title']);

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              That bit of code outputs the query string in <strong> tags but the title isn't changed!?!

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                OK, what is an example value of $q and $row['title']?
                Also, how do you output the $title? Is it just echo($title)?

                Comment

                • ziycon
                  Contributor
                  • Sep 2008
                  • 384

                  #9
                  Yes $title is just being echoed.

                  Examples below:

                  $q = 'dog';
                  $row['title'] = 'The dog went to the park today';

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    I have just tried:
                    Code:
                    	$q = "dog";
                    	$row['title'] = "the dog went to the park";
                    	$x = "<strong>" . $q . "</strong>";
                    	$title = str_replace($q , $x , $row['title']);
                    	echo($title);
                    And it worked fine. Also tried:
                    Code:
                    	$row = array("cat went home","dog was dead","mouse likes cheese");
                    	$q = "dog";
                    	$x = "<strong>" . $q . "</strong>";
                    	$title = str_replace($q , $x , $row[1]);
                    	echo($title);
                    Which also worked. If these are not working on yours then your PHP is not working properly. What version are you using?

                    Comment

                    • ziycon
                      Contributor
                      • Sep 2008
                      • 384

                      #11
                      I don't understand, it's not working for my, PHP is working fine, been using it for over 3 years now!!

                      Comment

                      • TheServant
                        Recognized Expert Top Contributor
                        • Feb 2008
                        • 1168

                        #12
                        OK, what about replacing <strong></strong> with <span style="font-weight: bold;"></span>. That will tell if it's the str_replace or <strong>. Are you sure you're not re-defining the font weight or properties somewhere else with another class or style? What is your search_results_ link_name class define?

                        Comment

                        • ziycon
                          Contributor
                          • Sep 2008
                          • 384

                          #13
                          Turns out that it was working to some extent, if the search was test the it would only highlight test that was exactly the same like test but wouldn't highlight Test, it was case sensitive. Thanks for the help.

                          Comment

                          • TheServant
                            Recognized Expert Top Contributor
                            • Feb 2008
                            • 1168

                            #14
                            No worries, glad you got it sorted out.

                            Comment

                            Working...