简体   繁体   English

将PHP转换为JS时,如何解决JavaScript不能输入中断的问题?

[英]How to fix javascript from entering a breakline when translating PHP to JS?

I'm currently trying to read lines in php, (it has to be php), and logging them. 我目前正在尝试读取php中的行(必须是php)并进行记录。 This works, as i'm getting 这有效,因为我越来越

$item1_name = $lines[1]; $ item1_name = $ lines [1]; ----> 'MY ITEM NAME' ---->'我的商品名称'

However, later on i have to use that $item1_name as a javascript variable: 但是,稍后我必须将$ item1_name用作javascript变量:

var item1 = ' < ? var item1 ='<? php echo $item1_name ? php echo $ item1_name? > '; >';

The problem is, as i am declaring my variable, the console will output a breakline, because in my txt file, i entered a new line to declare the $item1_size. 问题是,当我声明变量时,控制台将输出一个换行符,因为在我的txt文件中,我输入了新行来声明$ item1_size。

Hence, my code will not work as javascript is experiencing an unexpected line break. 因此,我的代码无法正常运行,因为javascript遇到意外的换行符。

I kinda have no idea how to disable javascript from executing a breakline, as my txt file has to have multiple lines. 我有点不知道如何禁用执行断行的javascript,因为我的txt文件必须有多行。

Help would be appreciated! 帮助将不胜感激!

Thank you 谢谢


SOLVED Trimming it directly in the variable has fixed it. 已解决直接在变量中修剪已将其固定。

Probably you are splitting the text file with \\n where the text file uses \\r\\n as the line breaks. 可能是用\\n拆分文本文件,其中文本文件使用\\r\\n作为换行符。 Try using: 尝试使用:

$item1_name = trim($lines[1]);

or 要么

var item1 = '<?php echo trim($item1_name);?>';

to eliminate the whitespace around your string. 消除字符串周围的空格。

PHP's trim function is structured like this: PHP的trim函数的结构如下:

trim ( string $str [, string $character_mask = " \\t\\n\\r\\0\\x0B" ] ) : string 修剪(字符串$ str [,字符串$ character_mask =“ \\ t \\ n \\ r \\ 0 \\ x0B”]):字符串

which means the second optional parameter containing the characters [space]\\t\\n\\r\\0\\x0B defines which characters to remove from the begining and the end. 这表示第二个可选参数包含字符[space]\\t\\n\\r\\0\\x0B用于定义[space]\\t\\n\\r\\0\\x0B和结尾删除的字符。 You can extend them to whatever you like. 您可以将它们扩展到您喜欢的任何地方。

http://php.net/manual/en/function.trim.php http://php.net/manual/zh/function.trim.php

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

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