• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

php - replace words with links

Hello,
I am using this code and its working perfectly fine to replace words inside posts in my forum. The problem that I am facing is that if the post doesn't contain those specific words then the post wont contain any link in it.. Is there any way instead of using keywords to just go through the post converting words randomly so that each post has links?

	$tolks = 'iphone,android,imac';	
	$hur_find_array    = explode (",", $tolks);
	$hur_replace_array = explode (",", $tolks);		
	$hur_find_array = array_filter($hur_find_array);
	$hur_find_array = array_values($hur_find_array);	
	$hur_replace_array = array_filter($hur_replace_array);
	$hur_replace_array = array_values($hur_replace_array);

	foreach ($hur_replace_array AS &$hurlink) {
		$hurlink = 'http://google.com';
		$hur_find_array[$huri] = trim ($hur_find_array[$huri]);
		$hurlink = '<a href="'.trim($hurlink).'"' . ' target="_blank" >$1</a>';
	}

	foreach ($hur_find_array AS &$hurfind) {
		$hurfind = preg_quote(htmlspecialchars($hurfind));
		$hurfind = '~('.$hurfind.')\b(?![^<]*(</a>|" />|width="\d+"|target="_blank"|</b></a>|</font></a>|</b></font></a>|</font></b></a>|width=\'\d+\'|follow">|rel="thickbox">|onclick="[\w .()]+"))~i';
	}

	$omessage = preg_replace ($hur_find_array, $hur_replace_array, $omessage);
  return $omessage;

Open in new window

0
XK8ER
Asked:
XK8ER
  • 7
  • 7
  • 3
1 Solution
 
Ray PaseurCommented:
...go through the post converting words randomly...
Eh?  Please give us a little more of the SSCCE, thanks.
0
 
Mark BradyCommented:
On line 11 ($hur_find_array[$huri])  $huri is not defined anywhere

Give us a list of the keywords you need to replace and what you want them replaced with. The logic doesn't seem right to me.
0
 
Mark BradyCommented:
You could do something like this.

<?php

$key_words = array(
    'iphone' => '<a href="http://www.iphone.com" target="_blank">iphone</a>',
    'android' => '<a href="http://www.android.com" target="_blank">android</a>',
    'imac' => '<a href="http://www.imac.com" target="_blank">imac</a>'
    );

$message = 'This is a test message. If you want to but an Imac just click on the link. To buy an Iphone you can do that as well or even buy an Imac. All these keywords will be replaced with links and those links will open up the apple site and point to the product you selected.';
echo $message.'<br /><br />';

// replace the keywords with the links
foreach ($key_words as $key => $link) {
    $message = str_ireplace($key, $link, $message);
}

echo $message;

?>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
XK8ERAuthor Commented:
yes but the purpose of posting in here is because I do not want to use keywords.. so having (iphone,android,imac)
I am looking for randomly selecting words and making them links..
0
 
XK8ERAuthor Commented:
my bad coding skills sorry,

$hur_find_array[$huri] = trim ($hur_find_array[$huri]);

should be removed..
0
 
Ray PaseurCommented:
... randomly selecting words and making them links ...
Maybe if you can tell us the business reason for this we could suggest something that would make sense.  Random links don't sound like something that would be beneficial to the client and would probably make the search engines avoid your site.

So what do you want to accomplish?
0
 
XK8ERAuthor Commented:
Ray_Paseur, I totally understand you but this is not for commercial purposes or anything like that.. this is just for learning purposes..
0
 
Mark BradyCommented:
....So.... can be elaborate a little more. Random links would be very easy to do but in most cases they would be dead links if that url did not exist. If you could open up more on your idea then maybe we could offer some good advice.
0
 
Ray PaseurCommented:
You can choose random words in an array of words fairly easily.  You can make a string of text into an array of words with explode().  You can use str_replace() to change a word into a link.  All of these things have been discussed and illustrated.  So at this point, what is the question?  What are you trying to learn to do?
0
 
XK8ERAuthor Commented:
you say samples have been posted here but I haven't seen not even one.. I just get suggestions of what I should make but I am not looking for suggestions I am looking to replace some words for links..

basically what im trying to have is about 5% of the text be converted into

<a href="/mypage.html">WORD-HERE</a>

so for example if the post has only 10 words having 5 words converted into links its too much.. but if the post has 100 words convert 5 words into links.
if the post has 1000 words convert 50 words into links.. etc.. I am trying really hard to explain what I am looking for I don't know why is it so hard to understand..
0
 
Ray PaseurCommented:
You can use count() to determine the number of words in an array of words (you have already learned about how to use explode() to create the array from the string).  You can use PHP arithmetic functions to know how many words you want to turn into links (multiply the value from count() by 0.5 and round() the answer to an integer.  You can use shuffle() and array_slice() to choose the words you want to turn into links.

What about duplicate words?  If there are 1,000 words in the input string, would you want to count all 1,000 even if there are a lot of duplicates?
0
 
XK8ERAuthor Commented:
Ray_Paseur, thanks for the info do you think you can post a little sample so that I can go by it?
0
 
Ray PaseurCommented:
I don't think I have a teaching example of anything like this.  It's not something anyone would do in "real life" so I'd be in the position of doing original research and writing code for free.  Professionals don't do that.  If you can't get the code started yourself, you might consider going to eLance or something like that.  It's not a large project and will not cost you much money.  If you've got some of the code working and need a hand getting "over a hump" please post what you've written and I'll try to help.
0
 
XK8ERAuthor Commented:
I've requested that this question be deleted for the following reason:

I found my own answer.. please delete..
0
 
Ray PaseurCommented:
I object to deleting this question.  We have worked hard with you to try to help understand the question and we've posted many examples and links that show how to do the various parts of what you want to achieve.  So instead of deleting it, please post your answer here so we can see how you answered it yourself.

Thanks and regards, ~Ray
0
 
XK8ERAuthor Commented:
I did not use any of your "posted examples" therefore I don't have to show anything.
I was able to just use explode " " to an array which is something very simple and then just convert words with strlen($words > 5) into links.. works like a charm for what I'm doing.
0
 
Ray PaseurCommented:
I'm glad you were able to accept Elvin66's answer.  However you might have saved yourself and everyone else a lot of work if you had just accepted it when it was offered instead of posting this pushback:
I am looking for randomly selecting words and making them links.
As anyone can plainly see the accepted solution does not offer random selection of words.

This is why we ask you to create the SSCCE.  It is the right way to ask a question.  And often in the course of consolidating your thoughts sufficiently to ask a question the right way, you may find that you answer your own question!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now