在没有PHP解释分隔符的情况下输出PHP分隔符()

时间:2022-05-07 20:42:00

I run PHP in JavaScript files, e.g....

我在JavaScript文件中运行PHP,例如....

var = '<?php /*some code*/ ?>';).

I need to use JavaScript to scan a string for PHP delimiters (the < ? php and ? > that open and close PHP).

我需要使用JavaScript来扫描PHP分隔符的字符串(<?php和?>打开和关闭PHP)。

I already know the code using JavaScript...

我已经使用JavaScript了解代码...

if (b.value.indexOf('<?php')>-1) {alert('PHP delimiter found.');}

What I'm having trouble with is that I need to keep the ability for PHP to be interpretted in JavaScript files (no exceptions). I simply need to output the delimiter strings to the client in JavaScript and not have them interpreted by the server.

我遇到的麻烦是我需要保持PHP文件中解释的能力(没有例外)。我只需要在JavaScript中将分隔符字符串输出到客户端,而不是由服务器解释它们。

So the final output (from the client's view) would be...

所以最终输出(来自客户的视图)将是...

if (b.value.indexOf('<?php')>-1) {alert('PHP delimiter found.');}

With the following code...

使用以下代码......

if (b.value.indexOf('<?php echo '<?php'; ?>')>-1 || b.value.indexOf('<?php echo '?>'; ?>')>-1)

I get the error: "Parse error: syntax error, unexpected T_LOGICAL_AND"

我收到错误:“解析错误:语法错误,意外T_LOGICAL_AND”

5 个解决方案

#1


1  

You could take advantage of Javascript's ability to parse hex as a character:

您可以利用Javascript将十六进制解析为字符的能力:

if (b.value.indexOf('<\x3fphp')>-1) {alert('PHP delimiter found.');}

In Javascript '<\x3fphp' is exactly the same thing as '<?php', but it has no meaning in PHP.

在Javascript'<\ x3fphp'中与'<?php'完全相同,但在PHP中没有任何意义。

#2


2  

Javascript will never find the <?php in your strings because simply, they have already been parsed by your PHP Server. Javascript is a client-side script and is executed after your server-side scripts.

Javascript永远不会在你的字符串中找到<?php,因为它们已经被PHP服务器解析了。 Javascript是一个客户端脚本,在服务器端脚本之后执行。

#3


0  

Use output buffering on the PHP, then htmlspecialchars() on the buffered output. You then search for the HTML entities with the JavaScript.

在PHP上使用输出缓冲,然后在缓冲输出上使用htmlspecialchars()。然后,您使用JavaScript搜索HTML实体。

#4


0  

the first thing that came into my mind and should work is this simple, little "cheat":

我想到的第一件事应该是这个简单,小小的“作弊”:

<?php echo '<'.'?php'; ?>

the php interpreter doesn't see a <?php, but the output is as desired.

php解释器没有看到<?php,但输出是所希望的。

#5


0  

<?php echo '<?php'; ?> ... <?php echo '?>'; ?>

<?php echo'<?php'; ?> ... <?php echo'?>'; ?>

#1


1  

You could take advantage of Javascript's ability to parse hex as a character:

您可以利用Javascript将十六进制解析为字符的能力:

if (b.value.indexOf('<\x3fphp')>-1) {alert('PHP delimiter found.');}

In Javascript '<\x3fphp' is exactly the same thing as '<?php', but it has no meaning in PHP.

在Javascript'<\ x3fphp'中与'<?php'完全相同,但在PHP中没有任何意义。

#2


2  

Javascript will never find the <?php in your strings because simply, they have already been parsed by your PHP Server. Javascript is a client-side script and is executed after your server-side scripts.

Javascript永远不会在你的字符串中找到<?php,因为它们已经被PHP服务器解析了。 Javascript是一个客户端脚本,在服务器端脚本之后执行。

#3


0  

Use output buffering on the PHP, then htmlspecialchars() on the buffered output. You then search for the HTML entities with the JavaScript.

在PHP上使用输出缓冲,然后在缓冲输出上使用htmlspecialchars()。然后,您使用JavaScript搜索HTML实体。

#4


0  

the first thing that came into my mind and should work is this simple, little "cheat":

我想到的第一件事应该是这个简单,小小的“作弊”:

<?php echo '<'.'?php'; ?>

the php interpreter doesn't see a <?php, but the output is as desired.

php解释器没有看到<?php,但输出是所希望的。

#5


0  

<?php echo '<?php'; ?> ... <?php echo '?>'; ?>

<?php echo'<?php'; ?> ... <?php echo'?>'; ?>