By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

commenting ?>

P: n/a
I cant think in any reason why this code fails...

Commented line shuldn't be completely ignored by the parser?

<?php

// ?whatever

ehco 'bla bla';

?>
In the real case, I'm having problems commenting out regex expressions
such as

$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?>#si', '<\1>',$str);

While this doesn't work:
// $res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?>#si', '<\1>',$str);

The following seems to be treated as expected

/*
$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?>#si', '<\1>',$str);
*/

Again, I can't see the reason of that parser's behavior...

regards- julian

Jul 7 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
*** julian_m escribió/wrote (7 Jul 2006 11:35:08 -0700):
I cant think in any reason why this code fails...

Commented line shuldn't be completely ignored by the parser?

<?php

// ?whatever
According to manual:

The "one-line" comment styles only comment to the end of the line or the
current block of PHP code, whichever comes first. This means that HTML code
after // ... ?or # ... ?WILL be printed: ?breaks out of PHP mode and
returns to HTML mode, and // or # cannot influence that.

It sounds like the logical option to me.

--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Jul 7 '06 #2

P: n/a
Alvaro G. Vicario wrote:
*** julian_m escribió/wrote (7 Jul 2006 11:35:08 -0700):
I cant think in any reason why this code fails...

Commented line shuldn't be completely ignored by the parser?

<?php

// ?whatever

According to manual:

The "one-line" comment styles only comment to the end of the line or the
current block of PHP code, whichever comes first. This means that HTML code
after // ... ?or # ... ?WILL be printed: ?breaks out of PHP mode and
returns to HTML mode, and // or # cannot influence that.

It sounds like the logical option to me.
I didn't know that, now I understand how it works, although IMO, the
line I commented was not de end of the current block of PHP:

//$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?>#si', '<\1>',$str);

Note that ?is a string there.

regards - julian

Jul 7 '06 #3

P: n/a
On 7 Jul 2006 13:02:11 -0700, "julian_m" <ju***********@gmail.comwrote:
>Alvaro G. Vicario wrote:
>According to manual:

The "one-line" comment styles only comment to the end of the line or the
current block of PHP code, whichever comes first. This means that HTML code
after // ... ?or # ... ?WILL be printed: ?breaks out of PHP mode and
returns to HTML mode, and // or # cannot influence that.

It sounds like the logical option to me.

I didn't know that, now I understand how it works, although IMO, the
line I commented was not de end of the current block of PHP:

//$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?>#si', '<\1>',$str);

Note that ?is a string there.
Ah, but it isn't in a string, because it's in a comment.

(Sounds circular, but actually it's not).

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jul 7 '06 #4

P: n/a

Andy Hassall wrote:
On 7 Jul 2006 13:02:11 -0700, "julian_m" <ju***********@gmail.comwrote:
Alvaro G. Vicario wrote:
According to manual:

The "one-line" comment styles only comment to the end of the line or the
current block of PHP code, whichever comes first. This means that HTML code
after // ... ?or # ... ?WILL be printed: ?breaks out of PHP mode and
returns to HTML mode, and // or # cannot influence that.

It sounds like the logical option to me.
I didn't know that, now I understand how it works, although IMO, the
line I commented was not de end of the current block of PHP:

//$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?>#si', '<\1>',$str);

Note that ?is a string there.

Ah, but it isn't in a string, because it's in a comment.

(Sounds circular, but actually it's not).
Right.
Even though someone could think "hey dude, just comment with /* */ and
go ahead". there are situations, dealing with regex, where it wouldn't
work. For instance:

/*
preg_replace('#(<\s*span\s*>\s*<\s*/\s*span\s*>|<\?xml\s*:\s*namespace.*?>|<\s*/?\s*o\s*:\s*p\s*>)#si','',...)
*/

It's really annoying isn't it?

regards - julian

Jul 7 '06 #5

P: n/a
On 7 Jul 2006 13:26:45 -0700, "julian_m" <ju***********@gmail.comwrote:
>For instance:

/*
preg_replace('#(<\s*span\s*>\s*<\s*/\s*span\s*>|<\?xml\s*:\s*namespace.*?>|<\s*/?\s*o\s*:\s*p\s*>)#si','',...)
*/

It's really annoying isn't it?
Yep, nasty.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Jul 7 '06 #6

P: n/a
julian_m (ju***********@gmail.com) wrote:

: Andy Hassall wrote:
: On 7 Jul 2006 13:02:11 -0700, "julian_m" <ju***********@gmail.comwrote:
: >
: Alvaro G. Vicario wrote:
:
: According to manual:
: >
: The "one-line" comment styles only comment to the end of the line or the
: current block of PHP code, whichever comes first. This means that HTML code
: after // ... ?or # ... ?WILL be printed: ?breaks out of PHP mode and
: returns to HTML mode, and // or # cannot influence that.
: >
: It sounds like the logical option to me.
:
: I didn't know that, now I understand how it works, although IMO, the
: line I commented was not de end of the current block of PHP:
:
: //$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?>#si', '<\1>',$str);

One solution is to split the string into pieces to break up the characters
and then concat the pieces.
$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?' . '>#si', '<\1>',$str);
^^^^^^

Or you can try to escape one or more of the characters to change the
string without changing its value. I'm not sure off hand if the php escape
works this way. What you want is to escape the character, which for
many characters is simply gives you the character itself (in some
languages, maybe in php).
$res= preg_replace('#<\s*(p|span|li|ul|ol)\s+.*?\>#si', '<\1>',$str);
^

Jul 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.