PHP Find and Replace characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lynel
Forum Newbie
Posts: 1
Joined: Mon Jun 23, 2003 2:39 pm

PHP Find and Replace characters

Post by lynel »

Can a person dump a text file into a database, then find and replace certain characters? How would you go about doing this...
Galahad
Forum Contributor
Posts: 111
Joined: Fri Jun 14, 2002 5:50 pm

Post by Galahad »

I'm not sure that this is what you are looking for, but you could retrieve the data from the database and store it in a php variable, use something like str_replace or preg_replace to make your changes, then put it back in the database with a mysql update query.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

The easiest option could be to find/replace in the text file itself.
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

string function

Post by Judas »

:idea:

Code: Select all

<?php
$file="<!--your_file-->";

$old = file ($file);

foreach ($old as $line_num => $line) {
  //$word=explode(" ",$line);
  //maybe an second loop with the word array
  $line[]= str_replace ("<!--find_this-->","<!--replace_with_this-->",$line);

  }

$new=implode("\n",$line);
?>
Post Reply