Re: regular expression
| From: | James Atkinson | Date: | Fri, 06 Oct 2000 22:10:17 +0000 |
| Subject: | Re: regular expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18995@lists.php.net to get a copy of this message | ||
On Fri, 06 Oct 2000, php@aeroinc.net wrote:
> The trouble with <code> is that it ignores whitespace.
> (At least with IE, didn't test with Netscape.)
>
> People will be posting a lot of code snippets, and I
> want it to preserve spacing like <pre>. But I'd rather
> have it break, like <code>. So maybe there'd be a way
> to convert all spaces within a <code></code> to ' '
> with regular expressions?
>
> I can resort to just looping through the string character
> by character, but I thought maybe it could be done easily
> with regular expressions.
>
> --
Hi, I'm the lead developer on a project called phpBB, its a UBB style message
board and we've implmented exactly what you're trying to do.
Here's how we handle [code] blocks.
if (preg_match("#\[code\](.*?)\[/code\]#si", $message, $matches)) {
$matches[1] = htmlspecialchars($matches[1])
$message = preg_replace("#\[code\](.*)\[/code\]#si", "<!-- BBCode
Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font
size=-1>Code:</font><HR></TD></TR><TR><TD><FONT
SIZE=-1><PRE>$matches[1]</PRE></FONT></TD></TR><TR>$
}
It formats it quite nicely. The HTML comments in there are just so its easy to
remove the HTML and replace it with [code] and [/code] for editing a message.
I have functions that replaces all the UBB code type things [b], [i], [url] etc
etc. if you want them:)
--
James Atkinson
-----------------------------------------------------------------
James Atkinson 100world
Technical Developer 512-1529 West 6th Avenue
phone: +1-604-637-8122 Vancouver, BC, V6J 1R1
fax: +1-604-742-1770 Canada
email: james.atkinson@100world.com http://www.100world.com
+---------------------------------------------------------------+
| This message may contain confidential and/or privileged |
| information. If you are not the addressee or authorized to |
| receive this for the addressee, you must not use, copy, |
| discolse or take any action based on this message or any |
| information herein. If you have received this message in |
| error, please advise the sender immediately by reply e-mail |
| and delete this message. Thank you for you cooperation |
+---------------------------------------------------------------+