Jump to content

Preg_replace thousands of arrays from table


surfwtw

Recommended Posts

 

Below is the code I have so far for only 2 terms with '$word" being the keyword of the site and all the other words are suppose to be linked if they appear in the text.  Okay I have thousands of different possible words from the same table. In the table 'word' is the term to be defined and 'definition' is the main content.  I want a code that recognizes any word that is listed in my table and replaces it.

 

<?php

 

 

$find2 = array ('/$word/i', '/bond/i');

$replace2 = array ('<a href=http://www.subjectmoney.com/definitiondisplay.php?word=$word><b>$word</b></a>', '<a href=http://www.subjectmoney.com/definitiondisplay.php?word=Bond>bond</a>');

 

 

 

Echo preg_replace ($find2, $replace2, $definition);

 

 

?>

Link to comment
Share on other sites

There's the easy, but more resource-consuming way:

 

make a php script that grabs every row from the table, do a while($row = mysql_fetch_assoc($result)) and in each iteration, do the preg_replace for the specified variable and then do a mysql UPDATE to modify that row

 

I know there's a way to do the search/replace using mysql(provided the $find2 in question is the same for every record) using a find/replace query, but I don't know mysql well enough to be able to help you with that

Link to comment
Share on other sites

I think you are making this hard on yourself.

 

If you have the words in the database already, why are you trying to add all the href links to them in the database, that's a lot more information than you need to store.

 

Just echo whatever result for word as a hyperlink on display.

 

echo "<a href='definitiondisplay.php?word=$word'><b>$word</b></a>";

 

I believe if you write what the complete goal are trying to accomplish, is a better way to do it.

Link to comment
Share on other sites

I want key words on my website to be linked for efficiency reasons for my customers and for SEO purposes.  When people are reading an article and they come across a word they don't know, I want it to be linked the way they are in wikipedia.

Link to comment
Share on other sites

I want key words on my website to be linked for efficiency reasons for my customers and for SEO purposes.  When people are reading an article and they come across a word they don't know, I want it to be linked the way they are in wikipedia.

 

You may not understand what he(who posted above you) meant. Anytime the text is displayed on a web page, it would convert the keyword into a link, so any web crawler would see the hyperlink. It's just in the table itself that there would be no link; and crawlers don't access database tables to index their results so his way is totally fine

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.