Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将html转换为一行字符串?_Php_Linux_Bash - Fatal编程技术网

Php 如何将html转换为一行字符串?

Php 如何将html转换为一行字符串?,php,linux,bash,Php,Linux,Bash,我有一些大的html模板: <% /* This is basically all the markup and interface /* %> <div id="test" class="test-right"></div> <div id="test" class="test-right"></div> 如果您正在寻找一种纯粹的bash方法来实现这一点,那么您可以在命令行中一次运行一个或在脚本中运行它们 # Stor

我有一些大的html模板:

<% /*   
  This is basically all the markup and interface
/* %>
<div id="test" class="test-right"></div>
  <div id="test" class="test-right"></div>

如果您正在寻找一种纯粹的
bash
方法来实现这一点,那么您可以在命令行中一次运行一个或在脚本中运行它们

# Store the contents of the file into a variable
fileContents="$(<file)"

# Create a temporary string using 'GNU mktemp'
tempfile="$(mktemp)"

# Parameter substitution syntax to replace the new-line character by
# empty string and store it in the file identified by the temporary
# name
printf "%s\n" "${fileContents//$'\n'//}" > "$tempfile"

# Revert the temp file to original file
mv "$tempfile" file

如果您想用普通的
\n
替换换行符(回车符),可以使用
awk

awk -v ORS='\\n' 1 file
这将把输出记录分隔符
ORS
替换为一个普通的
\n

1
触发
awk
中的默认操作,打印整个记录。

如果该html用于进一步渲染换行符,则应将其替换为

\n
否需要该html代码的框架,他希望该代码为“\n”非
# Store the contents of the file into a variable
fileContents="$(<file)"

# Create a temporary string using 'GNU mktemp'
tempfile="$(mktemp)"

# Parameter substitution syntax to replace the new-line character by
# empty string and store it in the file identified by the temporary
# name
printf "%s\n" "${fileContents//$'\n'//}" > "$tempfile"

# Revert the temp file to original file
mv "$tempfile" file
awk -v ORS="" 1 file
<% /*     This is basically all the markup and interface/* %><div id="test" class="test-right"></div>  <div id="test" class="test-right"></div>
awk -v ORS='\\n' 1 file