July 9, 2012

Replacing keywords with similar content

Question by Samson

With a list of keywords like in the array below:

$keywordlist = array(
    'Apple iPhone' => 'http://www.example.com/apple/iphone/',
    'Apple iPad' => 'http://www.example.com/apple/ipad',
    'Samsung Galaxy Ace' => 'http://www.example.com/samsung/galaxy-ace',
    'Samsung Galaxy Nexus' => 'http://www.example.com/samsung/galaxy-nexus',
    'Samsung' => 'http://www.example.com/samsung/',
    'Apple' => 'http://www.example.com/apple/'
);

I want to replace the keywords with the URLs associated with them.

I’ve tried looping through them and use str_replace and preg_replace, but this is replacing the manufacturers name in all keywords, so all the keywords get turned into links for just ‘Samsung’ and ‘Apple’. I’m a bit stumped as to where to head next, anyone got any pointers?

Edit:

I used the below code to loop through –

foreach($keywordlist as $name => $link){ 
    $content = str_replace($name, '<a href="'.$link.'">'.$name.'</a>', $content);
}

Solution:

I believe the problem was the link text I was replacing. It was then being replaced again by the other phrases with similar keywords, I’ve got the below to work.

Anyone think of a better way of doing this?

$content = "<p>This Apple iPhone is from Apple</p>
            <p>This Apple iPad is from Apple</p>
            <p>This Samsung Galaxy Ace is from Samsung</p>
            <p>This Samsung Galaxy Nexus is from Samsung</p>
            <p>This is a Samsung</p>
            <p>This is an Apple</p>";



$keywordlist = array(
    'Apple iPhone' => '[1]',
    'Apple iPad' => '[2]',
    'Samsung Galaxy Ace' => '[3]',
    'Samsung Galaxy Nexus' => '[4]',
    'Samsung' => '[5]',
    'Apple' => '[6]'
);

$content = str_replace(array_keys($keywordlist), array_values($keywordlist), $content);

$urllist = array(
    '[1]' => '<a href="http://www.example.com/apple/iphone/">Apple iPhone</a>',
    '[2]' => '<a href="http://www.example.com/apple/ipad">Apple iPad</a>',
    '[3]' => '<a href="http://www.example.com/samsung/galaxy-ace">Samsung Galaxy Ace</a>',
    '[4]' => '<a href="http://www.example.com/samsung/galaxy-nexus">Samsung Galaxy Nexus</a>',
    '[5]' => '<a href="http://www.example.com/samsung/">Samsung</a>',
    '[6]' => '<a href="http://www.example.com/apple/">Apple</a>'
);

$content = str_replace(array_keys($urllist), array_values($urllist), $content);

echo $content;

Output:

This Apple iPhone is from Apple

This Apple iPad is from Apple

This Samsung Galaxy Ace is from Samsung

This Samsung Galaxy Nexus is from Samsung

This is a Samsung

This is an Apple

Answer by Starx

str_replace() should be enough for what you are attempting.

foreach($keywordlist as $key => $link) {
     $content = str_replace($key, '<a href="$link">$key</a>', $content);
}

As, I have commented, this will not work as expected because of the repeated duplicate keywords on the keys.

You need to have a fixed format to denote a key. The one I suggest is something like [Apple] and [Apple iPad]. When you implement something similar to this, every keyword will be different, although they contain same inner code inside.

Your updated keyword structure will look something this, after wards.

$keywordlist = array(
    '[Apple iPhone]' => 'http://www.example.com/apple/iphone/',
    '[Apple iPad]' => 'http://www.example.com/apple/ipad',
    '[Samsung Galaxy Ace]' => 'http://www.example.com/samsung/galaxy-ace',
    '[Samsung Galaxy Nexus]' => 'http://www.example.com/samsung/galaxy-nexus',
    '[Samsung]' => 'http://www.example.com/samsung/',
    '[Apple]' => 'http://www.example.com/apple/'
);

If this, options is not feasible to use, as this significantly increase the complexity while developing the content, as needing to wrap every keyword text with [].

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!