Solved

php - replace words with links

Posted on 2013-05-12
17
325 Views
Last Modified: 2013-05-12
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
Comment
Question by:XK8ER
  • 7
  • 7
  • 3
17 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39159411
...go through the post converting words randomly...
Eh?  Please give us a little more of the SSCCE, thanks.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 39159499
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
 
LVL 20

Accepted Solution

by:
Mark Brady earned 500 total points
ID: 39159521
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:XK8ER
ID: 39159846
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39159847
my bad coding skills sorry,

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

should be removed..
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39159872
... 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
 
LVL 1

Author Comment

by:XK8ER
ID: 39159912
Ray_Paseur, I totally understand you but this is not for commercial purposes or anything like that.. this is just for learning purposes..
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 39159929
....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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39159940
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39159966
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39159992
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39160021
Ray_Paseur, thanks for the info do you think you can post a little sample so that I can go by it?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39160140
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39160154
I've requested that this question be deleted for the following reason:

I found my own answer.. please delete..
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39160155
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39160161
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39160186
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question