d45ndx
d45ndx

Reputation: 89

PHP generated Js function calls are created with line breaks

I'm using PHP to call a Js function with values generated by PHP.

$fp = fopen($_FILES['file']['tmp_name'], 'rb');

while(($line = fgets($fp)) !== false)
{
    $split = explode(":", $line);

    echo '
    <script type="text/javascript">
        var a = updateHashes("' . $split[0] . '", "' . $split[1] . '"); 
        console.log(a); 
    </script>'; 
}

But my code adds some line breaks to the code, which cause errors as you can see in the following screenshot:

enter image description here

What could I do to fix this?

Upvotes: 0

Views: 44

Answers (2)

dparoli
dparoli

Reputation: 9161

You can try to use the php trim() function on each line, that should solve.

$split = explode(":", trim($line));

Upvotes: 1

labue
labue

Reputation: 2623

Add trim()

echo '
    <script type="text/javascript">
        var a = updateHashes("' . trim($split[0]) . '", "' . trim($split[1]) . '"); 
        console.log(a); 
    </script>'; 

Upvotes: 0

Related Questions