Forum Moderators: coopster

Message Too Old, No Replies

Search and replace multiple strings

Script needed

         

shaundunne

12:11 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Anybody know any good php scripts for replaceing multiple strings?

I want to have a table containing two columns; the string to find and the string to replace it with.

ayushchd

2:04 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



well, i have been out of touch with php for quite long..have been really bz wid studies..

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.

shaundunne

2:23 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Cheers,

That looks like it'll do the job.

shaundunne

2:27 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Im wondering,

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

ayushchd

2:57 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



What exactly do you want?

Is the string you want to make replacements into a user input from a textarea or a text box?

You want to replace the words before getting them into the db?

Do you want to replace the words with links or make them appear as hyperlinked?

Please explain, maybe I can help you.

shaundunne

3:14 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Is the string you want to make replacements into a user input from a textarea or a text box?

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.

ayushchd

3:36 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Again, In one of the columns, you can have the string, say example, and in the other column you have the url, say example.com..and the user input is $_POST[user_input]

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);
}

shaundunne

3:57 pm on Jan 4, 2008 (gmt 0)

10+ Year Member



Thanks , that looks like the buisness. Ill try it out over the weekend, thanks very much.

ayushchd

6:46 am on Jan 5, 2008 (gmt 0)

10+ Year Member



Just one correction..

$user_input = $_POST[user_input] should be outside the loop ;)

wheelie34

10:34 am on Jan 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do this on a few sites to replace around 25 words/phrases

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