0

I am using preg_replace for buzzword type functionality, using a list of buzzwords, searching for them in an article and then using preg_replace to wrap a link around them.

My buzzwords are:

Online Marketing
Social Media Markeing
Social Media
Social
Brand Marketing
Brand
Search Engine Optimisation
SEO
Email Marketing
Email
Twitter
Facebook
LinkedIn

and I am doing my preg_replace like this...

$text = preg_replace("/Online Marketing/", "<a href='".$base."online-marketing/'>Online Marketing</a>", $text);

but the issue is I am having to do a preg_replace for each possible variation like so...

$text = preg_replace("/Online Marketing/", "<a href='".$base."online-marketing/'>Online Marketing</a>", $text);
$text = preg_replace("/Online marketing/", "<a href='".$base."online-marketing/'>Online marketing</a>", $text);
$text = preg_replace("/online marketing/", "<a href='".$base."online-marketing/'>online marketing</a>", $text);

Which is extremely long winded, now I know that I can use an array with str_repalce like this:

$from = array("Online Marketing", "Online marketing", "online marketing");
$name = str_replace($from, "", $text);

But I cannot see a way of finding which part of the array was matched and then replacing it with the correct link, as you can see the str_replace above just totally deletes my buzzword, its just an example.

So I was wondering if anyone would be able to help me find a way of using an array of buzzwords in a str_replace or preg_replace but also make sure that it repalces the buzzword with the correct link.

Hope someone can help

1
  • I have got it all working now and finished, thanks all, I will post code tomorrow showing how I did it as i need to wait 8 hours because im only 61 Rep Oct 4, 2011 at 15:55

4 Answers 4

2
$test = preg_replace("/online marketing/i", "<a href='".$base."'online-marketing/'>$0</a>, $text);

Or, if maintaining capitalisation is not important:

$test = str_ireplace("online marketing", "<a href='".$base."'online-marketing/'>online marketing</a>, $text);
5
  • I see so /i will do a search for the string in all case types? what about the output? will it recognise where the capital letters are and output them in the same place too? Oct 4, 2011 at 15:02
  • $0 will be where the match is put, case is preserved. Oct 4, 2011 at 15:03
  • @AdriftUniform: Yes, it will replace $0 with the string that was matched, exactly as it was. (Ps. Kolink, you have some double quotes missing from your examples.) Oct 4, 2011 at 15:04
  • brilliant thanks! so would it be possible to do something like... $array = ("online marketing", "social media marketing", "brand marketing") preg_replace("/$array/i", "<a href='".$base."'online-marketing/'>$0</a>, $text); Oct 4, 2011 at 15:05
  • You'd need to run a foreach and preg_replace one buzzword at a time. Oct 4, 2011 at 15:07
0

Function preg_replace can receive a second array of replacements. If you are going to use always the same syntax, you can do a loop to generate the link (instead of writing it yourself each time you want to had an element)

$base = 'base/';
$from = array("Online Marketing", "Online test", "offline test");
$to = array();
foreach($from AS $key=>$f) {
   $from[$key] = "/".$f."/";    
   $to[$key]="<a href='".$base.$f."/'>online marketing</a>";
}
$string = 'this is a test using Online Marketing and offline test';
$text = preg_replace($from, $to, $string);
1
  • i see what your doing here but i dont think its quite what im looking for, thanks tho Oct 4, 2011 at 15:11
0

You can use str_ireplace() with arrays (which is a case insensitive match string replacement), so if a certain match is found, it will replace it with the corresponding replacement in the other array.

EG:

$needles = array("Online Marketing", "Online Games", "Online Shopping");

$replacements = str_replace('<a href="online_marketing.html>Online Marketing</a>"',
                             '<a href="online_games.html">Online Games</a>',
                             '<a href="online_shopping.html">Online Shopping</a>');

    $replaced = str_ireplace($needles, $replacements);

    // Show the replaced text
    echo $replaced;
);
1
  • i need it to be case sensitive so i will be using pre_replace ("/.../i", "", $text); Oct 4, 2011 at 15:13
0

I took a mixture of all of your answers and this is my finished result...

$Fbuzzwords = array(" online marketing", " social media marketing", " social-media-marketing", " social media", 
                " social", " brand marketing", " brand", " search engine optimisation", " seo", " email marketing", 
                " email", " e-mail marketing", " e-mail", " tiwtter", " facebook", " linkedin");

$Ubuzzwords = array("online-marketing", "social-media-marketing", "social-media-marketing", "social-media-marketing", 
                "social-media-marketing", "brand-marketing", "brand-marketing", "search-engine-optimisation", 
                "search-engine-optimisation", "online-marketing", "online-marketing", "online-marketing", 
                "online-marketing", "social-media-marketing", "social-media-marketing", "social-media-marketing");

$i = 0;

foreach ($Fbuzzwords as $buzzword)
{
    $url = $Ubuzzwords[$i];

    $Rbuzzwords = array(" <a href='".$base.$url."/'>online marketing</a>", " <a href='"
.$base.$url."/'>social media marketing</a>", 
                " <a href='".$base.$url."/'>social-media-marketing</a>", " <a href='".$base.$url."/'>social media</a>", 
                " <a href='".$base.$url."/'>social</a>", " <a href='".$base.$url."/'>brand marketing</a>", 
                " <a href='".$base.$url."/'>brand</a>", " <a href='".$base.$url."/'>search engine optimisation</a>", 
                " <a href='".$base.$url."/'>seo</a>", " <a href='".$base.$url."/'>email marketing</a>", 
                " <a href='".$base.$url."/'>email</a>", " <a href='".$base.$url."/'>e-mail marketing</a>", 
                " <a href='".$base.$url."/'>e-mail</a>", " <a href='".$base.$url."/'>tiwtter</a>", 
                " <a href='".$base.$url."/'>facebook</a>", " <a href='".$base.$url."/'>linkedin</a>");

    $replacement = $Rbuzzwords[$i];

    $text = preg_replace("/$buzzword/i", "$replacement", $text);

    $i++;
}

So I have 3 arrays, 1 for finding the word on the page, and 2 for the url, I loop though all of the text for each part of the array making sure I catch every case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.