Jump to content

How to use preg_replace


doc1355

Recommended Posts

Hi:

I have file a.csv that contains multiple columns and data. I want to replace all "</br>" tags in file a.csv with "<br />" and then save it to file b.csv

 

I am using the following code but it is not working:

 

<?

// File to open
$file = 'a.csv';

// Search and Replace Arrays
$search = array('</br>');
$replace = array('<br />');

// Open the file
$lines = file($file);

// Read through the file
foreach($lines as $line_num => $line) {
    $text = preg_replace($search, $replace, $line);
}

// Save the file
$save = $text->save ("b.csv");

?>

Link to comment
https://forums.phpfreaks.com/topic/136628-how-to-use-preg_replace/
Share on other sites

Ok, I'm using this code:

 

<?

// File to open
$file = 'a.csv';

// Search and Replace Arrays
$search = array('</br>');
$replace = array('<br />');

// Open the file
$lines = file($file);

// Read through the file
foreach($lines as $line_num => $line) {
    $text = str_replace($search, $replace, $line);
}

// Save the file
$save = $text->save ("b.csv");

?> 

 

and I'm getting this error:

 

Fatal error: Call to a member function save() on a non-object in C:\localhost\test\process\replace.php on line 18

 

Apperantly the save function is not working.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.