问题,在HTML中返回脚本,需要第三'“


Issue, echoing HTML with script in it, in need of third ' "

<?php
echo '
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>
'; 
?>
如你所见,这在 处出现了冲突
<a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>

<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>

由于脚本开始'结束echo ',你会如何解决这个问题?对不起,如果这是一个愚蠢的问题

试试这个。这是可行的。

echo '
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$(''input[id=image_file]'').click();">Browse</a>
</div>
<script type="text/javascript">
$("input[id=image_file]").change(function() {
$("#photoCover").val($(this).val());
});
</script>
'; 
?>

看起来您正在寻找heredoc语法。Heredoc语法类似于第三种类型的'"

echo <<<HEREDOC
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>
HEREDOC;

<<<UNIQUEIDENTIFIER开始您的heredoc语句,然后以无空格结束,UNIQUEIDENTIFER,可能是;,然后是换行符。关闭它的语法是非常特殊的,所以要小心使用。

见:http://www.php.net/manual/en/language.types.string.php language.types.string.syntax.heredoc

注意:您不必使用HEREDOC作为您的唯一标识符,但我倾向于这样做,因为它使那些不熟悉语法的人更容易有一些东西谷歌来弄清楚他们看到的是什么

简单的回答是使用反斜杠转义,如下所示:

<?php
echo '
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
<input id="photoCover" class="input-large" type="text">
<a class="btn" onclick="$(''input[id=image_file]'').click();">Browse</a>
</div>
<script type="text/javascript">
$(''input[id=image_file]'').change(function() {
   $(''#photoCover'').val($(this).val());
});
</script>
'; 
?>

您可以这样转义'":

$html = ' " '" '' ';

这样你可以得到3个不同的报价:

$html = '<script type="text/javascript">
   $(''input[id=image_file]'').change(function() {
   $(''#photoCover'').val($(this).val());
   });
</script>';

在javascript代码中使用转义引号时也要小心,它位于PHP字符串中:

$html = '<script type="text/html">
    var = ''This text has ''''quoted'''' single quotes :)'';
</script>';

我不知道你为什么要在PHP中重复这个,但你可以这样做:

<?php
    // PHP Code
    // In a PHP file, anything outside of PHP tags just gets echoed
?>
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>
<?php
    // PHP Code
?>

您也可以使用Heredoc语法:

<?php
$html = <<<HTML
<input type="file" name="image_path" style="visibility:hidden;" id="image_file">
<div class="input-append">
  <input id="photoCover" class="input-large" type="text">
    <a class="btn" onclick="$('input[id=image_file]').click();">Browse</a>
</div>
<script type="text/javascript">
   $('input[id=image_file]').change(function() {
   $('#photoCover').val($(this).val());
   });
</script>
HTML;
echo $html;
?>

在这种情况下,你不需要担心引号和转义

由于这是PHP字符串结构中常见的事情,因此我将保留示例:

echo 'I want this text to show my ' in the sentence';

上面的例子是一个语法错误,因为字符串在预期之前关闭。但是,如果字符串中的单引号被转义,它将是有效的,并显示为:

echo 'I want this text to show my '' in the sentence';
//output : I want this text to show my ' in the sentence

其他方法是在双引号和单引号之间来回切换:

echo "This will show my ' in the sentence";
//output : This will show my ' in the sentence

但有时你需要更多,这样做看起来很难看:

echo "I want this ' in here but I need to quote ".'"poney"'." in my sentence";
//output : I want this ' in here but I need to quote "poney" in my sentence

所以要记得转义。使用HTML的另一种方法是退出PHP来显示它(有时不是一个选项):

<?php if(true) : ?>
<p>Show some html <?php echo $var;?> here</p>
<?php endif; ?>

<?php
    //some code here
?>
<html>
</html>

这可以通过多种方式实现,并且可以避免在echo

中使用引号。