简体   繁体   English

输出PHP分隔符( <?php, ?> )没有PHP解释分隔符

[英]Output PHP delimiter (<?php, ?>) without PHP interpreting the delimiters

I run PHP in JavaScript files, eg... 我在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). 我遇到的问题是我需要保持PHP在JavaScript文件中解释的能力(没有例外)。 I simply need to output the delimiter strings to the client in JavaScript and not have them interpreted by the server. 我只需要在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”

Javascript will never find the <?php in your strings because simply, they have already been parsed by your PHP Server. Javascript永远不会在你的字符串中找到<?php ,因为它们已经被PHP服务器解析了。 Javascript is a client-side script and is executed after your server-side scripts. Javascript是一个客户端脚本,在服务器端脚本之后执行。

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. 在Java '<\\x3fphp''<?php'完全相同,但是在PHP中没有意义。

Use output buffering on the PHP, then htmlspecialchars() on the buffered output. 在PHP上使用输出缓冲,然后在缓冲输出上使用htmlspecialchars()。 You then search for the HTML entities with the JavaScript. 然后,您使用JavaScript搜索HTML实体。

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 ,但输出是所希望的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM