Professional Web Applications Themes

regular expressions conditional replace - PHP Development

hi i am trying to make a bbcode pr and i get stuck at this :( i want to replace all '\n' with "<br />\n" in a text unless it's in between <pre></pre> tags. i can't figure out a way to conditionally replace anything. for example, this text: --- some text here <pre>some more text here !</pre> outside lala --- would become, if it was working: --- some text here<br /> <pre>some more text here !</pre> outside<br /> lala<br /> --- now all i have is: $step4 = preg_replace("#\n#is", "<br />\n", $step3); which replaces all newlines, even inside <pre> please ...

  1. #1

    Default regular expressions conditional replace

    hi

    i am trying to make a bbcode pr and i get stuck at this :(

    i want to replace all '\n' with "<br />\n" in a text unless it's in
    between <pre></pre> tags. i can't figure out a way to conditionally
    replace anything.

    for example, this text:
    ---
    some text here
    <pre>some
    more
    text here !</pre>
    outside
    lala
    ---

    would become, if it was working:
    ---
    some text here<br />
    <pre>some
    more
    text here !</pre>
    outside<br />
    lala<br />
    ---

    now all i have is:
    $step4 = preg_replace("#\n#is", "<br />\n", $step3);

    which replaces all newlines, even inside <pre>

    please help!
    Mig21 Guest

  2. #2

    Default Re: regular expressions conditional replace

    Mig21 wrote: 

    <?php

    $text =<<<EOT
    some text here
    <pre>some
    more
    text here !</pre>
    outside
    <pre> Inside </pre>
    lala
    EOT;

    $split = preg_split(
    "|(<pre>.*</pre>)|Uis",
    $text,
    -1,
    PREG_SPLIT_DELIM_CAPTURE
    );

    foreach (array_keys($split) as $i) {
    if (!stristr($split[$i], "<pre>")) {
    $split[$i] = nl2br($split[$i]);
    }
    }

    $text = implode("", $split);
    print $text;

    ?>


    JW



    Janwillem Guest

  3. #3

    Default Re: regular expressions conditional replace

    Mig21 wrote: 

    If you're getting this problem, you're writing your pr back to
    front. You need to split on the <pre> tags first, and then p each
    section separately.

    --
    Oli
    Oli Guest

  4. #4

    Default Re: regular expressions conditional replace

    Janwillem Borleffs a écrit :
     

    thanks a bundle! learn something new every day they say :)
    Mig21 Guest

  5. #5

    Default Re: regular expressions conditional replace

    Janwillem's solution is the best way to do it, but if I follow it
    correctly, if (!stristr($split[$i], "<pre>") is not optimal. <pre>
    will be at the beginning of $split[$i] so you can use if
    (substr($split[$i], 0, 5) !== '<pre>') instead. stristr() will search
    the entire string.

    Even if you did need to determine if <pre> is anywhere in the string
    according to the PHP manual you should "use the faster and less memory
    intensive function strpos() instead."

    Andrew Guest

  6. #6

    Default Re: regular expressions conditional replace

    Andrew wrote:
     

    Good point, although I choose to use stristr to make a case-insensitive
    match. To follow the manual's suggestion I would do it as follows:

    if (false !== stripos($split[$i], "<pre>")) {
    ...
    }


    JW


    Janwillem Guest

  7. #7

    Default Re: regular expressions conditional replace

    Janwillem Borleffs wrote:
     

    Hmmm... not very useful when you don't use PHP5. To make a
    case-insensitive match in PHP4, just follow Andrew's suggestion combined
    with strtolower:

    if (strtolower(substr($split[$i], 0, 5)) !== '<pre>') {
    ...
    }


    JW


    Janwillem Guest

Similar Threads

  1. MX 2004 - Find&Replace -> Regular expressions?
    By PJ in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 7th, 06:58 PM
  2. Regular Expressions :(
    By Liuk in forum PHP Development
    Replies: 8
    Last Post: January 8th, 09:27 PM
  3. PHP regular expressions
    By Albert in forum PHP Development
    Replies: 9
    Last Post: October 30th, 09:32 PM
  4. REPLACE with regular expressions?
    By Drew Seale in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: June 30th, 09:51 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139