Please help me with PREG_MATCH

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Please help me with PREG_MATCH

    Hi all,

    I need a help to write a simple preg_match.
    Imagine, I've got like 5kb of text where one part of the text always

    starts with: [quote:129beec637]
    and ends with: [/quote:129beec637]

    Number after "[quote:" is always different and changing its length.

    I need to parse this and display it using different color.
    Just so, when I display the text, people could figure out where the
    quotation starts and ends.

    Could someone please help me with it?
    Thanks a lot. Every help is very appreciated and helps me a lot.

    Joe



    Krakatioison Guest

  2. Similar Questions and Discussions

    1. trouble with preg_match()
      Can anyone tell me why the following evaluates to false preg_match("/^.*X$/",$composited_rod_end_head_p) when ...
    2. preg_match
      Hello, I would like to split a String containing ingredience with additional assigned footnotes... my problem I only get the first ingredience...
    3. preg_match allowing a-z AND å ä ö ü
      I've been searching the net and Google news groups for a preg_match expression that will return true on strings containing (uppercase and...
    4. preg_match bug
      Hey I wanted to see if anyone was getting the same problem. Using preg_match in the following pattern: "/<tr bgcolor="#FFFFFF">(.|\n)+?<\/tr>/" ...
    5. preg_match issue
      I have a function that checks the syntax of a string using preg_match. This function is used in a loop and always hangs on the same string (7 out of...
  3. #2

    Default Re: Please help me with PREG_MATCH

    A pattern like this would do the job:
    '/\[quote:.*\](.*)\[\/quote:.*\]/'

    preg_match('/\[quote:.*\](.*)\[\/quote:.*\]/', $string, $result);
    $result[1...] will hold what is between the quote

    But if I understand what you want to do, I would use preg_replace instead.
    Something like that:

    preg_replace('/\[quote:.*\](.*)\[\/quote:.*\]/', "<span
    style=\"color:blue\">$1</span>", $string);

    It also get rid of the [quote:...] and [/quote:...]

    Dae


    "Krakatioison" <com> a écrit dans le message de
    news:417e3b2e$com... 


    daniel Guest

  4. #3

    Default Re: Please help me with PREG_MATCH

    A little correction:
    The previous pattern would match [quote:...][/quote:..] that doesn't
    match... it's not good
    here is one that should fix that:
    '/\[quote:(.*)\](.*)\[\/quote:.\\1]/'

    preg_replace("/\[quote:(.*)\](.*)\[\/quote:\\1\]/", "<div
    style=\"color:blue\">$2</div>", $a);

    Dae


    "daniel arsenault" <ca> a écrit dans le message
    de news:iytfd.77183$videotron.net... 
    >
    >[/ref]


    daniel Guest

  5. #4

    Default Re: Please help me with PREG_MATCH

    "daniel arsenault" <ca> wrote: 

    Whilst this will work for some situations, it won't incorporate line-breaks
    in the quote text, it is case-sensitive (i.e. you can't use [QUOTE:...]),
    and doesn't check for invalid quotes (e.g. [quote:Z!"]).

    It also doesn't match [quote] tags by number, and is greedy. For instance,
    an input string of:

    Some words [quote:1]HELLO[/quote:1] more words
    [quote:2]GOODBYE[/quote:2] even more words

    will only have one quote block converted, between [quote:1] and [/quote:2].

    Oli.


    Oli Guest

  6. #5

    Default Re: Please help me with PREG_MATCH

    > Whilst this will work for some situations, it won't incorporate
    line-breaks 

    I totally forgot case and newlines, thanks.
    For the check I don't really know what are valid characters so I'll assume
    letters and digits:

    Here would be the new pattern:
    "/\[quote:([a-z0-9]*)\](.*)\[\/quote:\\1\]/is"
    Possible use:
    preg_replace("/\[quote:([a-z0-9]*)\](.*)\[\/quote:\\1\]/is", "<div
    style=\"color:blue\">$2</div>", $a);


     
    [/quote:2].


    This one I fixed it by using backreference to make sure that opening and
    closing [quote] match


    Dae


    daniel Guest

Posting Permissions

  • You may not post new threads
  • You may 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