Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
I have two questions about regular expressions in PHP.
#1:
What would the REGEXP for checking for [quote=someone]? I can do the replacing myself.
#2:
What would the REGEXP for checking for URLs and replacing them be? Here I need the replacing part too.
Any help would be very appreciated.
|
03.10.04 18:22 Post #1 | [Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
tundraH
Statusless Send PM Posts: 566
Threads: 20 Money: £1.26 (D) (+ Friend)
|
The easiest way would be to download phpBB and look at includes/bbcode.php.
|
03.10.04 22:02 Post #2 | [tundraH.com] [Hide Sig (0)] [Profile] [Quote] |
CBWhiz
Looking For Status Send PM Posts: 1044
Threads: 130 Money: £1343.20 (D) (+ Friend)
|
\n
<?PHP\n
//[quote]
$findq = "/[quote](.+?)[/quote]/si";
$replq = "<br><div class=quote><font size=1 color=#888888><b>Quote:</b></font><br>$1</div>";
//[quote=name] why was a dot not allowed in the name??
$findqn = "/[quote=(.+?)](.+?)[/quote]/si";
$replqn = "<br><div class=quote><font size=1 color=#888888><b>Quoted :: $1</b></font><br>$2</div>";
//quotes must be done recursively
while (preg_match($findq,$str)) {
$str = preg_replace($findq,$replq,$str);
}
while (preg_match($findqn,$str)) {
$str = preg_replace($findqn,$replqn,$str);
}
\n?>
\n
|
04.10.04 02:07 Post #3 | Last edited: 04.10.04 02:08 (CBWhiz - 1 times) |
[Hide Sig (3)] [Profile] [Quote] |
ReadMe
Absent Send PM Posts: 2820
Threads: 85 Money: £43.42 (D) (+ Friend)
|
of course you'll need to escape the [s since they are reserved for character classes.
I'd also export much neater code if i were you, throw in some line breaks, indentation and valid markup =p
________________
Cant be arsed to remake my sig. |
05.10.04 16:46 Post #4 | [Hide Sig (7)] [Profile] [Quote] |
CBWhiz
Looking For Status Send PM Posts: 1044
Threads: 130 Money: £1343.20 (D) (+ Friend)
|
Now that I think about that, thats true
COncidering i copieed and pasted form this forum's code, not sure why its working right tnow
|
06.10.04 04:03 Post #5 | [Hide Sig (3)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
It doesn't work, and adding the escape characters doesn't do any difference! I get the errors "Warning: Unknown modifier 'q' in c:\program\easyphp\www\sbg\functions.php on line 138" and "Warning: Unknown modifier 'q' in c:\program\easyphp\www\sbg\functions.php on line 142" for every post in the thread.
Those lines are the lines where the two whiles start.
Quotes just show up as [ quote=someone ]something[ /quote ]. The same goes for [quote]-style quotes.
|
06.10.04 14:36 Post #6 | Last edited: 06.10.04 18:16 (Dingbats - 1 times) |
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
ReadMe
Absent Send PM Posts: 2820
Threads: 85 Money: £43.42 (D) (+ Friend)
|
can i see exactly what regex you've used?
________________
Cant be arsed to remake my sig. |
06.10.04 17:03 Post #7 | [Hide Sig (7)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
\n
<?PHP\n
$find = "/[quote](.+?)[/quote]/si";
$repl = "<br><div class='quote'><font size='1' color='#888888'><b>Quote:</b></font><br>$1</div>";
$findn = "/[quote=(.+?)](.+?)[/quote]/si";
$repln = "<br><div class='quote'><font size='1' color='#888888'><b>Quote :: $1</b></font><br>$2</div>";
//Quotes must be done recursively
while(preg_match($find, $string))
{
$string = preg_replace($find, $repl, $string);
}
while(preg_match($findn, $string)) <br />
{
$string = preg_replace($findn, $repln, $string);
}
return $string;\n?>
\n
|
06.10.04 18:17 Post #8 | Last edited: 06.10.04 18:19 (Dingbats - 2 times) |
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
ReadMe
Absent Send PM Posts: 2820
Threads: 85 Money: £43.42 (D) (+ Friend)
|
\n
<?PHP\n
$find = "/[quote](.+?)[/quote]/si";
$repl = "<br><div class='quote'><font size='1' color='#888888'><b>Quote:</b></font><br>$1</div>";
$findn = "/<br><div class='quote'><span class='quoteheader'>Quoted :: (.+?)</span><br>(.+?)[/quote]/si";
$repln = "<br><div class='quote'><font size='1' color='#888888'><b>Quote :: $1</b></font><br>$2</div>";
//Quotes must be done recursively
while(preg_match($find, $string))
{
$string = preg_replace($find, $repl, $string);
}
while(preg_match($findn, $string)) <br />
{
$string = preg_replace($findn, $repln, $string);
}
return $string;\n?>
\n
you needed to escape the / in since you were using / as your delimiter. I always use ' cos it's neater and less likely to appear in a pattern, ie:
"'(.+?)'si"
________________
Cant be arsed to remake my sig. |
06.10.04 18:36 Post #9 | [Hide Sig (7)] [Profile] [Quote] |
Dingbats
; Send PM
Posts: 1970
Threads: 50 Mood: Optative Money: £268.57 (D) (+ Friend)
|
I'll try that.
Edit: It works Thank you!
|
07.10.04 05:46 Post #10 | Last edited: 07.10.04 13:36 (Dingbats - 1 times) |
[Softbrain Games] [Hide Sig (2)] [Profile] [Quote] |
Your Comments: