Highlighting the keyword.

Discussion in 'PHP' started by protocol96, Feb 11, 2007.

  1. #1
    Hello All,

    I am looking to write code which displays:
    from which keyword search a person landed on my website and highlighting them, if somebody has written it already, it would be really great if u can share Or can you please guide me how to go about it.


    Thanks in Advance,
    PHP Newbie.
     
    protocol96, Feb 11, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    <?php
    
    $your_text = 'Your text. Here will the keywords be highlighted.';
    
    @extract(@parse_url($_SERVER['HTTP_REFERER']));
    
    if (preg_match('/((fr|g)oogle|msn|yahoo|alexa)/i', $host) AND preg_match('/q(uery)?=([^&]+)/i', $query, $keywords))
    {
    	$keywords = array_unique(preg_split('/[\s\+]+/', rawurldecode($keywords[2]), -1, PREG_SPLIT_NO_EMPTY));
    	
    	foreach ($keywords AS $keyword)
    	{
    		$your_text = preg_replace('/('. preg_quote($keyword) .')/i', '<span style="color: red; font-weight: bold;">$1</span>', $your_text);
    	}
    }
    
    echo $your_text;
    
    ?>
    
    PHP:
    Give this a try.
     
    nico_swd, Feb 11, 2007 IP
  3. protocol96

    protocol96 Peon

    Messages:
    413
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is there a way I can chk this locally:confused:
    or i gotta try this on live site only?
     
    protocol96, Feb 11, 2007 IP
  4. ColdMoney

    ColdMoney Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this! if you need to run on server please comment first line and uncomment second line $url :D

    <?php
    
    $url="http://www.google.com/search?q=spyware&meta=";
    //$url=$_SERVER['HTTP_REFERER'];
    @extract(@parse_url($url));
    if (preg_match('/((fr|g)oogle|msn|yahoo|alexa)/i', $host) AND preg_match('/q(uery)?=([^&]+)/i', $query, $keywords)){    
    $keywords = array_unique(preg_split('/[\s\+]+/', rawurldecode($keywords[2]), -1, PREG_SPLIT_NO_EMPTY)); 
    
           foreach ($keywords as $keyword)    {        
    	   echo "You come from $host with keyword [".$keyword."]";
    	   }
     }
    
    ?>
    PHP:
     
    ColdMoney, Feb 11, 2007 IP
    protocol96 likes this.
  5. protocol96

    protocol96 Peon

    Messages:
    413
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks ColdMoney, it did run, but i faced 2 issues,
    1. It doesnt work for multiple keywords
    Correct me if I am wrong, its got to do somthing with regular expression
    and
    2. It aint highlighting the keyword on the page.:D
    I will try my R&D tomorrow.
     
    protocol96, Feb 11, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I made a fuction of it.

    
    <?php
    
    function highlight_search_keywords($text)
    {
            @extract(@parse_url($_SERVER['HTTP_REFERER']));
           
            if (preg_match('/((fr|g)oogle|msn|yahoo|alexa)/i', $host) AND preg_match('/[qp]=([^&]+)/i', $query, $keywords))
            {
                    $keywords = preg_split('/[\s\+\,]+/', rawurldecode($keywords[1]), -1, PREG_SPLIT_NO_EMPTY);
                   
                    foreach (array_unique($keywords) AS $keyword)
                    {
                            $text = preg_replace('/('. preg_quote($keyword) .')/i', '<span style="color: red; font-weight: bold;">$1</span>', $text);
                    }
            }
           
            return $text;
    }
    
    
    ?>
    
    PHP:
    http://www.bytemycode.com/snippets/snippet/588/

    It's easier to use this way. And yes, it highlights multiple keyworlds.
     
    nico_swd, Feb 11, 2007 IP
  7. protocol96

    protocol96 Peon

    Messages:
    413
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7

    Yup, it worked, thanx a lot for this mate
     
    protocol96, Feb 11, 2007 IP