Php 如何将html转换为一行字符串?
我有一些大的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
<% /*
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