Solved

php - replace words with links

Posted on 2013-05-12
17
320 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 108

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
 
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 108

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

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 108

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 108

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 108

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 108

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now