Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Simple regex not working ?

    Hi,

    I am trying to change dashes to underscores in text file.

    The reason is that I am having problems processing some xml tags
    so I want to change this <merchant-name> to <merchant_name>

    To do this I used this regex:

    PHP Code:
    $pattern '#<(\w+)-(\w+)>#';
    $replacement '<$1_$2>';
    $source preg_replace$pattern$replacement$source, -$count); 
    But for some reason, nothing is changed

    Have I misunderstood something ?

    The idea is that <anything-here> gets changed to <anything_here>

    There is data between the tags that should not be touched, it is just the tag
    names themselves that I want to change.

    In the file, there about 2000 tags which need changing.

    Can anyone see what I am doing wrong ?


    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Your regexp should work, except it does not take into account closing tags. You could try something like this:
    PHP Code:
    $source '<root><merchant-name>text</merchant-name><merchant-name>text 2</merchant-name></root>';
    $pattern '#<(\/?\w+)-(\w+)>#';
    $replacement '<$1_$2>';
    $source preg_replace$pattern$replacement$source, -$count);  
    echo 
    $source
    It is quickly written though. It could be that you could need something more complicated.

    Also I've supposed the the XML document is well-formed and does not contain CDATA-sections.

  • #3
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    This seems like overkill. What's wrong with a simple str_replace() for this?

    PHP Code:
    $source=str_replace("-","_",$source); 
    Edit: Nevermind, I didn't READ.

    There is data between the tags that should not be touched, it is just the tag
    names themselves that I want to change.
    Last edited by Rowsdower!; 03-19-2010 at 02:05 PM.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,683
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks for input,
    I amended my script to include the closing tags .)

    But I notice that when there are tow hyphens, then
    they do not get changed e.g. <sales-page-link>

    Is there a reason for this, and how can I overcome it ?

    Thanks.





    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by jeddi View Post
    The reason is that I am having problems processing some xml tags
    so I want to change this <merchant-name> to <merchant_name>
    If I recall correctly, using {merchant-name} rather than just merchant-name in your XML parser should sort the hyphen issue, i.e: enclose all id's/names with {}.

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Quote Originally Posted by jeddi View Post
    Thanks for input,
    I amended my script to include the closing tags .)

    But I notice that when there are tow hyphens, then
    they do not get changed e.g. <sales-page-link>

    Is there a reason for this, and how can I overcome it ?

    Thanks.
    You could use the e-modifier. Something like this:
    PHP Code:
    <?php
    $source 
    '<root><sales-page-link>text</sales-page-link><sales-page-link>text 2</sales-page-link></root>';
    $pattern '#(<\/?[\w-]+>)#e';
    $replacement 'str_replace("-","_","$1")';
    $source preg_replace$pattern$replacement$source, -$count);  
    echo 
    $source;  
    ?>
    Please notice: You should be very careful when using the e-modifier. Because in this case preg_replace() executes string as PHP code. It is as dangerous as using eval(). You must be absolutely that on one could inject any PHP code to the function and so execute his own PHP code on your system.
    Last edited by SKDevelopment; 03-19-2010 at 02:17 PM.


  •  

    Posting Permissions

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