# validate_file() and strip_file()

- Thread: https://externals.io/message/5559
- Original archive: https://news-web.php.net/php.internals/5559
- Started by Ilia Alshanetsky on 2003-11-19T16:24:56+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #5559 — Ilia Alshanetsky — 2003-11-19T16:24:56+00:00

URL: https://externals.io/message/5559#5559

Attached file, implements validate_file(), which implements a php space syntax 
check for PHP scripts. It returns a boolean true/false value indicating 
whether the script has parse errors or not. Essentially a "php -l" from 
within PHP.

strip_file() is added for completion (might as well :) ) and implements "php 
-w", which strips comments & whitespace from a PHP file. A quick obfuscater 
of sorts.

Any comments?

Ilia

---

## #5560 — Jan Lehnardt — 2003-11-19T16:30:31+00:00

URL: https://externals.io/message/5559#5560 · In reply to: #5559

Hi,

> Any comments?

I find that useful for something I am writing right now and I was about 
to
propose that myself :). I'd go for php_check_syntax() or at least
file_validate() and `php_strip_whitespace()` or at least
file_strip_whitespace(), but that's not too important here.

+1 if anyone cares,
Jan
--
Q: Thank Jan? -  A: http://geschenke.an.dasmoped.net/    | 
Editor-in-Chief |
"We cats are very independent. We need nobody,  no time, |  PHP 
Magazine   |
  no where, no way. Isn't that right Pooky?" - Garfield
Key Fingerprint 7BCC EB86 8313 DDA9 25DF 1805 ECA5 BCB7 BB96 56B0

---

## #5566 — Andi Gutmans — 2003-11-19T18:33:00+00:00

URL: https://externals.io/message/5559#5566 · In reply to: #5559

I think it's a very bad idea to start copying lexer code into basic_functions.c
I don't have time to look into it but there must be a cleaner way of doing it.

Andi

At 11:24 AM 11/19/2003 -0500, Ilia Alshanetsky wrote:
>Attached file, implements validate_file(), which implements a php space 
>syntax
>check for PHP scripts. It returns a boolean true/false value indicating
>whether the script has parse errors or not. Essentially a "php -l" from
>within PHP.
>
>strip_file() is added for completion (might as well :) ) and implements "php
>-w", which strips comments & whitespace from a PHP file. A quick obfuscater
>of sorts.
>
>Any comments?
>
>Ilia

---

## #5567 — Andi Gutmans — 2003-11-19T18:33:28+00:00

URL: https://externals.io/message/5559#5567 · In reply to: #5559

At 05:30 PM 11/19/2003 +0100, Jan Lehnardt wrote:
>Hi,
>
> > […]
>
>I find that useful for something I am writing right now and I was about to
>propose that myself :). I'd go for php_check_syntax() or at least
>file_validate() and `php_strip_whitespace()` or at least
>file_strip_whitespace(), but that's not too important here.
>
>+1 if anyone cares,

If we're talking about naming the how about script_*?

Andi

---

## #5569 — Ilia Alshanetsky — 2003-11-19T18:48:16+00:00

URL: https://externals.io/message/5559#5569 · In reply to: #5567

> If we're talking about naming the how about script_*?

script_* is good, I have no naming preference. I am more interested to know 
whether this functionality is something we need or not...

Ilia

---

## #5573 — Hartmut Holzgraefe — 2003-11-19T19:11:36+00:00

URL: https://externals.io/message/5559#5573 · In reply to: #5569

Ilia Alshanetsky wrote:
> script_* is good, I have no naming preference. I am more interested to know 
> whether this functionality is something we need or not...

it is not needed, you can just do

   system("php -l filename");

instead ;)

actually i would love to have php_check_syntax() as
it would simplify my CVS commit check scripts but i
would like to have it return the actual error message
as string on errors

IMHO it should be php_check_syntax() and not
script_check_syntax() to make clear what it actualy
checks (it won't check the syntax of my shell, awk
or perl scripts, would it? ;)

-- 
Hartmut Holzgraefe  <hartmut@php.net

---

## #5575 — Jon Parise — 2003-11-19T19:19:45+00:00

URL: https://externals.io/message/5559#5575 · In reply to: #5574

> Good idea, we already have some function that utilize php_ prefix, might as 
> well continue with the trend. How about php_lint() & php_strip()?
 
php_lint() sounds goodr, and I'm cool with either php_strip() or
 `php_strip_whitespace()`.

I can see these functions being useful in various (albeit rare)
situations.

-- 
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)

---

## #5574 — Ilia Alshanetsky — 2003-11-19T19:23:44+00:00

URL: https://externals.io/message/5559#5574 · In reply to: #5573

> Ilia Alshanetsky wrote:
> > […]
>
> it is not needed, you can just do
>
>    system("php -l filename");
>
> instead ;)

Sure, but that's slow and more importantly, not everyone has access to php-cgi 
/ php-cli to perform this action. Heck, many people do cannot use 
exec,shell_exec,system,etc... functions because their providers disallow 
execution funcitons.

> IMHO it should be php_check_syntax() and not
> script_check_syntax() to make clear what it actualy
> checks (it won't check the syntax of my shell, awk
> or perl scripts, would it? ;)

Good idea, we already have some function that utilize php_ prefix, might as 
well continue with the trend. How about php_lint() & php_strip()?

---

## #5581 — mfischer@gjat.josefine@Markus Fischer — 2003-11-19T19:40:50+00:00

URL: https://externals.io/message/5559#5581 · In reply to: #5559

On Wed, Nov 19, 2003 at 11:24:56AM -0500, Ilia Alshanetsky wrote : 
> Attached file, implements validate_file(), which implements a php space syntax 
> check for PHP scripts. It returns a boolean true/false value indicating 
> whether the script has parse errors or not. Essentially a "php -l" from 
> within PHP.

	As someone else already suggested, providing the error message would
	be a good (=foreseeing) idea.

	Maybe it's possible to have something like:

	bool php_check_syntax($filename [, &$error_message]);

	which returns the error message as the second parameter.


	As a sidenote, would streams be supported (http, ftp) ? Or are there
	some kind of limitating factors?

	- Markus

---

## #5583 — Wez Furlong — 2003-11-19T20:16:47+00:00

URL: https://externals.io/message/5559#5583 · In reply to: #5582

It should work out of the box.

--Wez.

> > […]
> 
> That should be possible as well.

---

## #5582 — Ilia Alshanetsky — 2003-11-19T20:18:34+00:00

URL: https://externals.io/message/5559#5582 · In reply to: #5581

> 	bool php_check_syntax($filename [, &$error_message]);

I'll need to look at the code, it should be possible to do. Although I'd 
prefer to have it return the error rather then return it by reference as an 
optional argument.

> 	As a sidenote, would streams be supported (http, ftp) ? Or are there
> 	some kind of limitating factors?

That should be possible as well.

Ilia

---

## #5601 — mfischer@gjat.josefine@Markus Fischer — 2003-11-20T05:51:51+00:00

URL: https://externals.io/message/5559#5601 · In reply to: #5582

On Wed, Nov 19, 2003 at 03:18:34PM -0500, Ilia Alshanetsky wrote : 
> 
> > […]
> 
> I'll need to look at the code, it should be possible to do. Although I'd 
> prefer to have it return the error rather then return it by reference as an 
> optional argument.

	I understand. I might think it's just safer so one can assume the
	return value (bool) just gives information whether it works or not
	and the error message itself is a bonus.

	if (!php_check_syntax("myscript.php", &$error_message) {
		die ( "myscript.php didn't validaet: $error_message");
	}

	vs.

	if (strlen($error_message = php_check_syntax("myscript.php")) > 0) {
		die ( "myscript.php didn't validaet: $error_message");
	}

	I find the former a somewhat cleaner approach than the latter; or
	maybe it's just me.

	- Markus
