Jump to content

search and replace over a file

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
dirdamalah0

dirdamalah0

    Newbie

  • Members
  • Pip
  • 1 posts
Hello

I have this regular expression:
</book>(?:\n)<collection>(.*)</collectioninfo>

And I have this peace of text on a FILE

<book bookid="3" title="the title 3" remaining = "50" price="100">
<reader readerid="1"><![CDATA[John]]></reader>
<reader readerid="2"><![CDATA[Michael]]></reader>
<reader readerid="3"><![CDATA[Peter]]></reader>
<reader readerid="4"><![CDATA[Maria]]></reader>
[B]</book>
<collectioninfo id="123">
<title>colletion 1</title>
<date>Jun 19 11:00</date>
<type>2</type>
<limit>100</limit>
</collectioninfo>
[/B]
<book bookid="4" title="the title 4" remaining = "50" price="100">
<reader readerid="1"><![CDATA[John]]></reader>
<reader readerid="2"><![CDATA[Michael]]></reader>
<reader readerid="3"><![CDATA[Peter]]></reader>
<reader readerid="4"><![CDATA[Maria]]></reader>
</book>

And i need replace the text in BOLD for:

</book>


So... using PHP how can I do that ... but saving the result in the same file..

I mean.. replace the current text on a file.. for the new one..

Thx.

Edited by Jordan, 21 June 2008 - 05:22 AM.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
After you have loaded the data into a string you can just run preg_replace on it. IE:

$yourString = "This is the string variable that holds your data above";
$search = '/\<collectioninfo id\=(.*?)\>(.*?)\<\/collectioninfo\>/is';

$yourString = preg_replace($search,'',$yourString);

This is entirely untested.