HTML文件中file_get_contents之后出现空格

m4rk

我有2个文件:script.html和index.php。

我的目的是通过JavaScript在HTML文件中生成文本(在这种情况下为用户所在的国家/地区)。这是此HTML文件的代码:

script.html

<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script language="Javascript">document.write(codehelper_ip.Country);</script>

现在,我想将该文本带到PHP文件中以将其用作变量:

index.php

<?php
$content = file_get_contents(script.html);
echo "http://www.example.com/$content/example"
?>

问题在于该回声的输出是奇数的:

回声输出:

http://www.example.com/ US /example

注意“ US”之前和之后的空格当然,这破坏了代码的全部目的。我不能有这样的网址。

有什么办法可以避免或删除这些空格?

为什么会这样呢?

我的猜测:由于JavaScript代码的换行符,正在插入这些空格。如果我没有在任何一个换行符<script></script>和另一个换行符之间放置任何换行符<script></script>(例如<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script><script language="Javascript">document.write(codehelper_ip.Country);</script>,全部换行),那么我会得到http://www.example.com/US /example不过,我不知道该如何避开后面的空间。

注意1:我已经尝试过trim()(我使用PHP 5+,所以应该可以使用)。我还尝试将文件扩展名“ .html”更改为“ .php”,以防万一它可以做某事,但事实并非如此。

注意2:即使我知道我可以用其他方法来做,但我想通过JavaScript而不是通过PHP获取用户所在的国家/地区。原因是我无法使用当前的托管计划通过服务器执行此操作,因此我想到了JavaScript。

非常感谢你。

博安

我认为您对何时执行各种脚本感到困惑。

$content = file_get_contents(script.html);

此时,$content包含<script>标签。由于尚未将JavaScript发送到浏览器,因此尚未执行JavaScript。

echo "http://www.example.com/$content/example"

这会将其发送到浏览器:

http://www.example.com/<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script language="Javascript">document.write(codehelper_ip.Country);</script>/example

由于PHP非常琐碎,请尝试使用以下简单的JavaScript解决方案:

<script language="Javascript" src="http://www.codehelper.io/api/ips/?js"></script>
<script language="Javascript">document.write("http://www.example.com/" + codehelper_ip.Country + "/example");</script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将javascript添加到php中由file_get_contents加载的html文件中

使用file_get_contents()在html文件中设置php变量

如何`file_get_contents()`多个文件?

file_get_contents编码出现问题

如果url有空格(对所有无效的文件进行编码),则file_get_contents为false

file_get_contents从Google获取的文件与浏览器中显示的文件不同

HTML/PHP 使用 file_get_contents() 在用户端获取 .txt 文件的内容

将文件中的数组(通过file_get_contents获取)转换为实际的php数组

file_get_contents从2个文本文件中添加数字

如何不使用file_get_contents读取PHP中的JSON文件?

如何替换file_get_contents中的变量以使用PHP生成CSS文件?

每次访问php文件时都在file_get_contents中更改url

file_get_contents从Facebook图像配置文件中随机提供较旧的图像

root拥有的文件的file_get_contents

php的file_get_contents是否忽略文件锁定?

GAE:file_get_contents()和静态文件

file_get_contents替代ajax上传中的大文件

错误,包括带有file_get_contents()的文件

在file_get_contents之前预处理文件

在file_get_contents('file.php')中获取变量

file_get_contents无法从正确的URL检索html

流明的file_get_contents

卡在file_get_contents

file_get_contents的超时在PHP中不起作用

在file_get_contents()php中插入Javascript

替换smarty模板中的php file_get_contents?

Go lang中的PHP file_get_contents

file_get_contents返回内容中的HTTP标头

如何处理file_get_contents()中的403错误?