Page 11 of 13
Posted: Fri Jun 15, 2007 7:28 pm
by ziggy3000
i just thought of a really good idea...
i use a preg_match_all on all text between /* and */
then i use foreach, and inside that, i use another preg_match_all that removes the html tags, which i posted the code for before.
would this work?
Posted: Fri Jun 15, 2007 7:34 pm
by superdezign
That could work if you code it right...
On the topic of unorthodox methods, why not preg_match_all the tags themselves and str_replace them out of the original?
Code: Select all
preg_match_all('/\/\*.*?(<[^>]+>)/', $str, $matches);
foreach($matches[1] as $tag)
{
str_replace($tag, '', $str);
}
Posted: Fri Jun 15, 2007 7:49 pm
by ziggy3000
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
ok i got this
Code: Select all
preg_match_all("'(/\*.*)(.*)(.*\*/)'", $sql, $matched, PREG_SET_ORDER);
foreach($matched[2] as $stuff){
preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $stuff, $match, PREG_SET_ORDER);
foreach($match as $var){
$tagb = $var[1];
$text = $var[3];
$tage = $var[4];
$sql = str_replace($tagb.$text.$tage, $text, $sql);
}
}
but when i try it, it says
invalid argument supplied for foreach()
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Jun 15, 2007 7:56 pm
by superdezign
Which one?
Always make sure that you check isset() or is_array(), as well.
Posted: Fri Jun 15, 2007 7:57 pm
by ziggy3000
the first one
Posted: Fri Jun 15, 2007 8:01 pm
by feyd
Your expression is quite greedy.
hint-hint.
Posted: Sat Jun 16, 2007 11:54 am
by superdezign
Lucky for you, I'm in the middle of making a parser, so I'm in a Regex kinda mood.
Code: Select all
preg_match_all('#/\*.*?\*/#', $str, $matches);
if(is_array($matches[0]))
{
foreach($matches[0] as $comment)
{
$str = str_replace($matches[0], preg_replace('#<([^>])>(.*?)</\1>#', '$2', $comment), $str);
}
}
It works fine.
Posted: Sat Jun 16, 2007 12:09 pm
by superdezign
By the way, I just thought of a way that this could have been pretty much solved using only CSS.
Code: Select all
.comment,
.comment * {
/* style */
}
Posted: Sat Jun 16, 2007 12:46 pm
by ziggy3000
simple solutions for complicated problems
i am sure i heard this before.

Posted: Sat Jun 16, 2007 1:01 pm
by superdezign
^_^
Funny, I didn't even think of it until I started writing a PHP parser and got to doing comments, and as I was writing the CSS I'm like "Ohh... he could have done this too."
Posted: Sat Jun 16, 2007 1:03 pm
by ziggy3000
i already made a parser which works really great. i am creating this so i can have it as an attachment for bbcode.

Posted: Sat Jun 16, 2007 11:30 pm
by ziggy3000
i got another problem
for the // and # type comments, it highlights everything but numbers, even with the css you gave me 2 posts ago. the number highlighting seems to end the the comment highlighting
here is my code
Code: Select all
$sql = preg_replace("'((//|#)(?:(\w|\s|\d))*[^<>])'is", "<span class='comment'>\\1</span>", $sql);
Posted: Sun Jun 17, 2007 7:33 am
by superdezign
Whoa, you're making that pattern much harder than it is. Non-capturing isn't what you want at all because you need the entire comment. Also, the 's' modifier makes your code not stop at newlines, but '//' and '#' comments
do stop at newlines. What you want is:
Code: Select all
$sql = preg_replace('|((//|#).*)|', '<span class="comment">$1</span>', $sql);
Posted: Sun Jun 17, 2007 7:40 am
by feyd
You have delimiter errors superdezign. It's also a good idea to make sure the pattern uses multi-line mode.
Posted: Sun Jun 17, 2007 7:49 am
by superdezign
Hehe, I wasn't sure if '|' could be a delimiter, but I figured that if it couldn't, ziggy would let me know.
And... if he used multiline... would he have to check against a newline character?