SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preg Replace, Maintain Space

    Hello,

    I have this working preg replace that replaces info within these comments. Is there a way to maintain the whitespace? Here is the code and examples:

    PHP Code:
    $regex "#<!--\s*(".$term.")\s*-->.*?<!--\s*END\s*".$term."\s*-->#is";
    $replace '<!-- $1 -->'.$rep.'<!-- END $1 -->';
    $newdoc preg_replace($regex$replace$document); 
    Example Code:
    Code:
                   <!-- COMMENT -->
                   Text
                   <!-- END COMMENT -->
    Desired Result:
    Code:
                   <!-- COMMENT -->
                   Replaced Text
                   <!-- END COMMENT -->
    Actual Result
    Code:
    <!-- COMMENT -->Replaced Text<!-- END COMMENT -->
    Thanks in advance!

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    $replace = '<!-- $1 -->'."\n$rep\n".'<!-- END $1 -->';

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to capture the whitespace then
    PHP Code:
     $regex "#<!--\s*(".$term.")\s*-->(\s*)([^\s]*)(\s*)<!--\s*END\s*".$term."\s*-->#is";
    $replace '<!-- $1 -->$2'.$rep.'$4<!-- END $1 -->'
    i dont see how you could be losing the whitespace before the first <!-- COMMENT --> though.

  4. #4
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im sorry I poorly explained the examples. You are correct clamcrusher the first comment spacing was not lost. Let me try your guys examples, thank you so much!

  5. #5
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think i found a problem with yours clamcrusher,

    It was not working correctly because sometimes the comments have more than one word so it doesnt match it, for example:

    Code:
    Code:
                   <!-- COMMENT HERE -->
                   Text here
                   <!-- END COMMENT HERE -->
    It seems your regex only works when there is only one word in the original comment, any more causes it to not work. Is there a way to modify it to get it to work like you had just for multiple words. Sometimes theres html in the comment as well so I dont know if that would screw up.

    Examples of code that need to be replaced so you can get an idea where im coming from.

    Original Code:
    Code:
                   <!-- COMMENT HERE -->
                   Text here!!!! <b>html</b></tr> stuff here##*(@
                   <!-- END COMMENT HERE -->
    Desired Result:
    Code:
                   <!-- COMMENT HERE -->
                   <b>NEW Comment here!!!#@)(#</b>
                   <!-- END COMMENT HERE -->
    Thank you for your replies!

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try changing ([^\s]*) to (.*?)

  7. #7
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you that worked!!!!

  8. #8
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have one more questoin clamcrusher:

    How can I replace the last occurance using preg:

    PHP Code:
    "#<!--\s*(".$term.")\s*-->(\s*)(.*?)(\s*)<!--\s*END\s*".$term."\s*-->#is"
    $replace '<!-- $1 -->$2'.$rep.'$4<!-- END $1 -->';
    $newdoc preg_replace($regex$replace$document1); 
    I think the limit only applies with the first occurance is there a way to reverse it and replace only the last occurance?

    Ill toss an example up:

    Code:
    Code:
                   <!-- COMMENT HERE -->
                   <b>NEW Comment here!!!#@)(#</b>
                   <!-- END COMMENT HERE -->
                   <!-- COMMENT HERE -->
                   <b>NEW Comment here!!!#@)(#</b>
                   <!-- END COMMENT HERE -->
                   <!-- COMMENT HERE -->
                   <b>NEW Comment here!!!#@)(#</b>
                   <!-- END COMMENT HERE -->
    Desired Result:
    Code:
                   <!-- COMMENT HERE -->
                   <b>NEW Comment here!!!#@)(#</b>
                   <!-- END COMMENT HERE -->
                   <!-- COMMENT HERE -->
                   <b>NEW Comment here!!!#@)(#</b>
                   <!-- END COMMENT HERE -->
                   <!-- COMMENT HERE -->
                   here is the new comment which only affected the end one
                   <!-- END COMMENT HERE -->
    Thank you!!

  9. #9
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know where I could find out how to do a reverse preg_replace with a limit of 1 time?

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im just curious, what exactly is the purpose of all of this?

    maybe there is a better way to go about it.

  11. #11
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A template manipulation so I can inject information between a commented html document to show them changes they make.

  12. #12
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    try changing ([^\s]*) to (.*?)
    I hate relying on laziness.

  13. #13
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a better way? It worked for me.

  14. #14
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well are the terms unique?

  15. #15
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No sometimes theres could be 10 to 20 of the same term within a comment throughout the document.

  16. #16
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well using lookaround would be nicer and more efficient but if you are happy it doesn't matter.


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
  •