Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to pick out some text for use in a Forum?

0 views
Skip to first unread message

703designs

unread,
Jul 29, 2008, 1:10:27 PM7/29/08
to
I'm writing an extension for some PHP forum software. $String is the
post contents, and I want to do the following to it. Note that I'm not
looking for someone to solve this for me, I'm just not sure what the
best approach is.

1. Find all instances of [code]user post text...[/code]
2. If the lang attribute is supplied, keep track of its value for that
code block, e.g. [code lang="css"]
3. Wrap the contents of the code block in HTML tags with the CSS class
set to lang's value if it was specified
4. Of course, output this new string in the same position as before in
the post

Any ideas? Thanks!

703designs

unread,
Jul 29, 2008, 1:12:34 PM7/29/08
to

Note that I don't need to actually output the text (the software
handles that), just return a $String value.

703designs

unread,
Jul 29, 2008, 1:39:23 PM7/29/08
to

Here's what I've come up with so far. Any recommended changes? It
seems to do the job pretty well.

$hlString = $String;
$hlString = str_replace('<', '&lt;', $hlString);
$hlString = str_replace('>', '&gt;', $hlString);
$hlString = preg_replace("/\[code( lang=\"([A-Za-z]+)\")?\]/", "<pre
name=\"code\" class=\"$2\">", $hlString);
$hlString = str_replace("[/code]", "</pre>", $hlString);
return $hlString;

gardnern

unread,
Jul 29, 2008, 1:40:22 PM7/29/08
to

the best way to do it is to use regular expressions to find the "lang"
as well as any text inside the [code][/code] tags.

703designs

unread,
Jul 29, 2008, 1:53:03 PM7/29/08
to

I forgot to note that I needed to convert < and > as well. My little
script works great! CSSForums.org will have syntax highlighting (using
SyntaxHighlighter, see http://code.google.com/p/syntaxhighlighter/)
for HTML, XML, CSS, JavaScript, PHP, and SQL once I finish polishing
this extension and install it this evening.

703designs

unread,
Jul 29, 2008, 2:15:52 PM7/29/08
to
On Jul 29, 1:53 pm, 703designs <thomasmal...@gmail.com> wrote:
> On Jul 29, 1:40 pm, gardnern <nat...@factory8.com> wrote:
>
>
>
> > On Jul 29, 12:10 pm, 703designs <thomasmal...@gmail.com> wrote:
>
> > > I'm writing an extension for some PHP forum software. $String is the
> > > post contents, and I want to do the following to it. Note that I'm not
> > > looking for someone to solve this for me, I'm just not sure what the
> > > best approach is.
>
> > > 1. Find all instances of [code]user post text...[/code]
> > > 2. If the lang attribute is supplied, keep track of its value for that
> > > code block, e.g. [code lang="css"]
> > > 3. Wrap the contents of the code block in HTML tags with the CSS class
> > > set to lang's value if it was specified
> > > 4. Of course, output this new string in the same position as before in
> > > the post
>
> > > Any ideas? Thanks!
>
> > the best way to do it is to use regular expressions to find the "lang"
> > as well as any text inside the [code][/code] tags.
>
> I forgot to note that I needed to convert < and > as well. My little
> script works great! CSSForums.org will have syntax highlighting (using
> SyntaxHighlighter, seehttp://code.google.com/p/syntaxhighlighter/)

> for HTML, XML, CSS, JavaScript, PHP, and SQL once I finish polishing
> this extension and install it this evening.

Sadly, I took a step back: My new Parse method doesn't support
converting newlines to <br />. If I use the usual:

$hlString = str_replace("\r\n", "<br />", $hlString);

My syntax highlighter goes haywire and gets <br />s inserted in place
of [code] newlines. How could I apply that replacement without
replacing "\r\n" contained in [code][/code]? Sounds like another Regex
problem, but these [code] blocks span multiple lines.

0 new messages