#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Replace strings with preg_replace


    Hello,

    I've been trying to replace some strings within a big string. This string contains some code blocks. The structure is basically something like this

    PHP Code:
    $str="<pre>some code</pre>some non code<pre>some code again</pre>..."
    I'd like to replace all the <pre>code</pre> by something like this <div style='..'><pre>code</pre><div>. Meanwhile, I should ideally be able to replace "code" by htmlspecialchars(code). I've tried something simple which is already not working:

    PHP Code:
    <?php
    $string 
    "<pre name=\"code\" class=\"brush:csharp;\">";
    $string .= "some code";
    $string .= "</pre>some non code<pre name=\"code\" class=\"brush:csharp;\">some other code</pre>";

    $regex "#([<]pre.*[>])(.*)([<]/pre[>])#";

    echo 
    preg_replace($regex,"<div style='background-color:black;color:white;'>$1$2$3</div>",$string);

    ?>
    with that, I get the following results in the returned HTML:

    Code:
    <div style='background-color:black;color:white;'><pre name="code" class="brush:csharp;">some code</pre>some non code<pre name="code" class="brush:csharp;">some other code</pre></div>
    So instead of replaing each <pre>..</pre>, it replaces only the first one and uses the last </pre> of the string...

    Any idea?
    Thanks
    Best Regards
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    This is enormously difficult and cannot be easily done.

    You have two options:
    1) Use the DOM to parse this document and change the DOM object tree into the tags you want, then turn it back into a string. This will break on malformed HTML.

    2) Step through the string one character at a time and dynamically count which tags you want and what their logical position is within the tag tree of this string.

    Neither one is easy, and there is no out-of-the-box solution for this.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    Hi,

    Thanks for your answer! I thought that it was possible with the regex and captures. The DOM stuff is out of question for me because I'm consuming that from mobile devices so I want to return directly the right HTML.

    Regarding the other approach, that might indeed be possible...

    Thanks for your answer but still expecting a magic regex that would do this.

    Thanks
    Best Regards
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    There isn't a regex that will work. We've tried before.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo