Hi,
I have an xml file which is not formatted in a convenient way.
I need to change the various references:
toCode:<col name="price">93147</col> <col name="value">93147</col>
in perl I would use something like:Code:<price>93147</price> <value>93147</value>
(untested!)Code:if ( $var =~ m|<col name="(.*?)">(.*?)</col>|g ) { $var =~ s|<col name=".*?">.*?</col>|<$1>$2</$1>|g; }
What is the best way of tackling this in php?
I would use the preg_replace function along with the same type of regular expressions that you would use in perl
Sorry - is this not the php forum?
Oh, my appologies, I thought you said in Perl!
What exactly are you trying to do? If your trying to decalair the names of the colums by using <tags> this wont work,
What you have already done is the correct way of doing it.Code:<col name="price">93147</col> <col name="value">93147</col>
Hi, thanks for your reply. I need the 1st example to end up as the second. There are many <col name= tags so I need the script to pick up the name and then replace with the new tags accordingly. I see preg_replace might be useful but how will I pick up the name tag?
sorry for my ignorance...
Could I see the rest of the script, code is good :3
outputs like I described above. If you know a bit of perl you'll understand what I'm after.Code:$strXML = get_content($strURLtoGo);
I need to search/replace on $strXML
Cheers
Solution:
Code:$pattern = '/<col name="(.*?)">(.*?)<\/col>/'; $replacement = '<$1>$2</$1>'; echo preg_replace($pattern, $replacement, $strXML);
Last edited by jonnyfolk; 03-03-2010 at 09:21 AM. Reason: solution found (myself)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks