Link to home
Start Free TrialLog in
Avatar of XK8ER
XK8ERFlag for United States of America

asked on

php - replace words with links

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 = '';
		$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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

...go through the post converting words randomly...
Eh?  Please give us a little more of the SSCCE, thanks.
Avatar of Mark Brady
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.
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of XK8ER


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..
Avatar of XK8ER


my bad coding skills sorry,

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

should be removed..
... 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?
Avatar of XK8ER


Ray_Paseur, I totally understand you but this is not for commercial purposes or anything like that.. this is just for learning purposes..
....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.
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?
Avatar of XK8ER


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..
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?
Avatar of XK8ER


Ray_Paseur, thanks for the info do you think you can post a little sample so that I can go by it?
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.
Avatar of XK8ER


I've requested that this question be deleted for the following reason:

I found my own answer.. please delete..
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
Avatar of XK8ER


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.
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!