Home > Archive > PHP Language > October 2004 > Please help me with PREG_MATCH
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Please help me with PREG_MATCH
|
|
| Krakatioison 2004-10-26, 8:55 am |
| 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
| |
| daniel arsenault 2004-10-26, 3:56 pm |
| 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" <klokan@sssbbbccc.com> a écrit dans le message de
news:417e3b2e$1_1@Usenet.com...
> 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
>
>
>
| |
| daniel arsenault 2004-10-26, 3:56 pm |
| 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" <arsenault.daniel@videotron.ca> a écrit dans le message
de news:iytfd.77183$vi5.1472377@weber.videotron.net...
> 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" <klokan@sssbbbccc.com> a écrit dans le message de
> news:417e3b2e$1_1@Usenet.com...
>
>
| |
| Oli Filth 2004-10-26, 3:56 pm |
| "daniel arsenault" <arsenault.daniel@videotron.ca> wrote:
> A pattern like this would do the job:
> '/\[quote:.*\](.*)\[\/quote:.*\]/'
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.
| |
| daniel arsenault 2004-10-26, 3:56 pm |
| > 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!"]).
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);
> 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].
This one I fixed it by using backreference to make sure that opening and
closing [quote] match
Dae
|
|
|
|
|