What I am trying to do here is call my php function from inside of a javascript function and it throws the unterminated string literal error from
var letters = "<?php echo GetRandLine('en_lang.txt'); ?>";
allthough the php function is returned correctly. What the php function is doing is returning a list of words to search from en_lang.txt.So from what I have gatherd it is processing the the <?php ?>
as the </script>
tag or is it something else.
php:
if (!function_exists('GetRandLine'))
{
function GetRandLine($name)
{
$file = fopen("$name","r");
$text = array();
$count = 0;
while(!feof($file))
{
++$count;
$text[] = fgets($file);
//echo fgets($file). "<br />";
}
fclose($file);
$n = rand(0,100);
$ntext = $text[rand(0,$count)];
//echo $n." ";
if($n > 50)
{
$ntext = $ntext . " " . $text[rand(0,$count)];
}
if($n > 80 && $n < 90)
{
$ntext = $ntext . " " . $text[rand(0,$count)];
}
if($n > 85 && $n < 88)
{
$ntext = $ntext . " " . $text[rand(0,$count)];
}
return $ntext;
}
}
javascript:
function clicker()
{
var letters = "<?php echo GetRandLine('en_lang.txt'); ?>";
document.getElementById('theinput').value = letters;
document.getElementById("clicker").click();
}
从GetRandLine返回的字符串中可能包含麻烦的字符,包括换行符,这会引起您提到的错误,您应该对其进行编码
var letters = <?php echo json_encode(GetRandLine('en_lang.txt')); ?>;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.