Forum Moderators: coopster
still..i'll give a try at this..
if you contain a table with the two columns which you described above...
u can have a variable, say $contents, which will contain the contents of the file you want to replace the strings in..
now (if i have got your question right), u can do this :
$sql = "select * from table;";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$contents = str_replace($row[replacement], $row[to_replace], $contents);
}
I guess this should do..only if I have got you question right.
What im looking for is a script that will search a string and replace certain words with links, much like google adwords.
I want a user to be able put in text into a field, and then when it is posted, certain defined words are replace with a link behind the words.
Anybody know if this is possible?
Cheers
The user input will be a text area.
You want to replace the words before getting them into the db?
No. I preferably want two versions in the same DB, maybe the same table. The original version that the user inputs and the version that gets updated.
Do you want to replace the words with links or make them appear as hyperlinked?
The words to become hyperlinked.
Please explain, maybe I can help you.
Then this can be the script :
$sql = "select * from table;";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$user_input = $_POST[user_input];
$replacement = $row[replacement];
$to_replace = '<a href="'. $row[url].'"> '.$replacement .'</a>';
$user_input = str_replace($replacement, $to_replace, $user_input);
}
$sql="select * from db where num='$num'";
$result = mysql_query($sql, $dblink) or die("System down");
$pairs = array(
'/two words/' => '<a href="http://www.example.com/">two words</a>',
'/word/' => '<a href="http://www.example2.com/">word</a>',
/* many pairs above */
);
while ($newArray = mysql_fetch_array($result)){
$num = $newArray['num'];
$title = $newArray['title'];
$story = preg_replace(array_keys($pairs), array_values($pairs), $newArray['story'], 1);
}
?>
The content of the database stays the same and only changes when the content is called to the page.