473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I replace this line ?

290 100+
Hi,

I have some data in a flat file called test_file.xml
that reads:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?><marketplace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.click2sell.eu/marketplace.xsd"><product>
And I want to change it so that it reads :

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?><marketplace><product>
What is the best way to do this ?

I assume I should open the file and get the contents:

Expand|Select|Wrap|Line Numbers
  1. $file_in = 'test_file.xml';
  2. $source = file_get_contents( $file_in  );
  3.  
Now should ıs use preg_match()

or would str_replace()

E.g can I have "wild characters" in the str_replace() ?


Expand|Select|Wrap|Line Numbers
  1. $source= str_replace('<marketplace xmlns*' , '<marketplace>', $source);
  2.  
  3. // Write it to out file
  4. file_put_contents( $file_out, $source );  
  5.  
Maybe I need to use preg_match()
to do this, or preg_replace() ??

Any advice, much appreciated :)



.
Feb 26 '10 #1
4 1504
rythmic
29
As far as I know str_replace does not accept wildcards

1. If you're comfortable with regular expressions preg_replace should do it.. basically you'd be looking for <marketplace<space>*>

2. you could parse it using php built-in xml parser functions. If element marketplace is found and it has attributes, you would replace that with a <marketplace> element without attributes.

info about xml parser is found here: http://www.php.net/manual/en/function.xml-parse.php

You could go through the file line by line and when condition is not met you just feed the original line and when one is found you do a replace.

3. Go through the document programmatically searching each line for
<marketplace<space>. When found, search for > ignore that part and insert the rest of the line. This is not all that time efficient but might be fun

I would definetley go with option one though as the coding required would be significantly less. Also the regexp required is not complicated so it should be quite efficient.
Feb 26 '10 #2
Markus
6,050 Expert 4TB
preg_replace() is overkill if you know what the string is that you need to replace.

Expand|Select|Wrap|Line Numbers
  1. $str = str_replace("<marketplace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.click2sell.eu/marketplace.xsd">", "<marketplace>", $str);
  2.  
Feb 26 '10 #3
jeddiki
290 100+
Thanks for reply,

I should have said - there is only one place where this occurs and it is right at the top of the file.

So is this what I need : ?

Expand|Select|Wrap|Line Numbers
  1. $file_in = 'test_file.xml';
  2. $file_out = 'test_output.xml';
  3.  
  4. $source = file_get_contents( $file_in  );
  5. $source = preg_replace( '#\<marketplace *\>#', '<marketplace>', $source);
  6.  
  7.  // Write it to out file
  8. file_put_contents( $file_out, $source );  

Hi Markus,


I am a bit concerned about that being so specific.

If they change the file schema name, it won't work.
Feb 26 '10 #4
Atli
5,058 Expert 4TB
I would recommend loading this up in a XML parser. It's less risk of accidentally messing up the markup.

Try using the DomElement::removeAttribute method, or something like that.
Feb 26 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
8
by: middletree | last post by:
What's wrong with this code? strLongDesc = Replace(Replace(Replace(Replace(Trim(Request.Form("LongDesc")),"'","''"),vbC rLf,"<br>"),"<",&lt;),"<",&gt;) Background: This field is a textarea, and I...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
3
by: gregpinero | last post by:
I'm trying to write a little script that will have a list of word pairs which will loop through that list and replace all instances of each word with the other word. I'm very new to javascript...
4
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
5
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
6
by: DataSmash | last post by:
Hello, I need to search and replace 4 words in a text file. Below is my attempt at it, but this code appends a copy of the text file within itself 4 times. Can someone help me out. Thanks! #...
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
5
by: Unknown | last post by:
Hi, I've got this code : cb = open("testfile", "r+") f = cb.readlines() for line in f: rx = re.match(r'^\s*(\d+).*', line) if not rx: continue else:
10
by: Raymond | last post by:
For some reason I'm unable to grok Python's string.replace() function. Just trying to parse a simple IP address, wrapped in square brackets, from Postfix logs. In sed this is straightforward given:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.