Closed Thread
Results 1 to 8 of 8

Thread: search and replace question

  1. #1
    jonnyfolk is offline Newbie
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    search and replace question

    Hi,

    I have an xml file which is not formatted in a convenient way.

    I need to change the various references:
    Code:
    <col name="price">93147</col>
    <col name="value">93147</col>
    to

    Code:
    <price>93147</price>
    <value>93147</value>
    in perl I would use something like:

    Code:
    if ( $var =~ m|<col name="(.*?)">(.*?)</col>|g ) {
      $var =~ s|<col name=".*?">.*?</col>|<$1>$2</$1>|g;
    }
    (untested!)

    What is the best way of tackling this in php?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: search and replace question

    I would use the preg_replace function along with the same type of regular expressions that you would use in perl

  4. #3
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: search and replace question

    I think you may have the wrong forum...

  5. #4
    jonnyfolk is offline Newbie
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Re: search and replace question

    Sorry - is this not the php forum?

  6. #5
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: search and replace question

    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,

    Code:
    <col name="price">93147</col>
    <col name="value">93147</col>
    What you have already done is the correct way of doing it.

  7. #6
    jonnyfolk is offline Newbie
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Re: search and replace question

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

  8. #7
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: search and replace question

    Could I see the rest of the script, code is good :3

  9. #8
    jonnyfolk is offline Newbie
    Join Date
    Feb 2010
    Posts
    9
    Rep Power
    0

    Re: search and replace question

    Code:
    $strXML = get_content($strURLtoGo);
    outputs like I described above. If you know a bit of perl you'll understand what I'm after.
    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)

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 03-18-2010, 10:15 AM
  2. Looking for search and replace feature.
    By Billboard in forum C and C++
    Replies: 0
    Last Post: 01-04-2010, 12:33 AM
  3. Website Search Engine Optimization Question
    By gyrly2 in forum Website Design
    Replies: 2
    Last Post: 05-15-2009, 08:04 AM
  4. VI: Search and Replace
    By Wanch in forum Linux Tutorials, Guides and Tips
    Replies: 2
    Last Post: 08-29-2008, 06:03 AM
  5. search and replace over a file
    By dirdamalah0 in forum PHP Development
    Replies: 1
    Last Post: 06-21-2008, 06:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts