如何处理这段与PHP相互呼应的HTML代码,它包含单引号和双引号?
我不确定这是否是一种更简洁的写作方式,但我认为我在这里没有问题:如何处理这段与PHP相互呼应的HTML代码,它包含单引号和双引号?,php,html,Php,Html,我不确定这是否是一种更简洁的写作方式,但我认为我在这里没有问题: <?php switch ( $meta_box['type'] ) { case 'textarea': echo '<textarea name="<?php echo $meta_box[ 'name' ]; ?>"> <?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="<?php echo $meta_box[ 'name' ]; ?>">
<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>
</textarea>'
break;
我倾向于对回音使用双引号,而在回音中使用单引号
echo "Hello string test ' ";
或者你可以用逃生梯\
echo "hello string test \" ";
同样,在代码中,您在echo中执行echo。我倾向于对echo使用双引号,而在该echo中使用单引号
echo "Hello string test ' ";
echo 'String with "double quotes" inside';
echo "String with \"double quotes\" inside";
echo 'String with \'single quotes\' inside';
echo "String with 'single quotes' inside";
echo 'String with \'single quotes\' and "double quotes" inside';
echo "String with 'single quotes' and \"double quotes\" inside";
或者你可以用逃生梯\
echo "hello string test \" ";
同样,在代码中,您在echo中执行echo。您可以像这样避开引号:
echo 'String with "double quotes" inside';
echo "String with \"double quotes\" inside";
echo 'String with \'single quotes\' inside';
echo "String with 'single quotes' inside";
echo 'String with \'single quotes\' and "double quotes" inside';
echo "String with 'single quotes' and \"double quotes\" inside";
echo "<input type=\"text\" name=\"test\" />;
echo”;
但是你为什么不这样做呢:
echo '<textarea name="' . $meta_box[ 'name' ] . '">
echo'
你可以避免这样的引用:
echo "<input type=\"text\" name=\"test\" />;
echo”;
但是你为什么不这样做呢:
echo '<textarea name="' . $meta_box[ 'name' ] . '">
echo'
如果您确实想输出PHP代码:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="<?=$meta_box[\'name\']?'.'>">
<?=htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ) ?'.'>
</textarea>';
break;
default:
echo '<input type="text" name="<?=$meta_box[ \'name\' ] ?'.'>"
value="<?=htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ) ?'.'>">';
?>
否则,这对我来说更有意义:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="'.$meta_box['name'].'>">'.
htmlspecialchars( $data[ $meta_box['name'] ] ).
'</textarea>';
break;
default:
echo '<input type="text" name="'.$meta_box[ 'name' ].'" '.
'value="'.htmlspecialchars( $data[ $meta_box[ 'name' ] ] ).'">';
}
?>
但是我猜$data[$meta_box['name']]
数组索引也不正确。如果您确实想输出PHP代码:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="<?=$meta_box[\'name\']?'.'>">
<?=htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ) ?'.'>
</textarea>';
break;
default:
echo '<input type="text" name="<?=$meta_box[ \'name\' ] ?'.'>"
value="<?=htmlspecialchars( $data[ $meta_box[ \'name\' ] ] ) ?'.'>">';
?>
否则,这对我来说更有意义:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="'.$meta_box['name'].'>">'.
htmlspecialchars( $data[ $meta_box['name'] ] ).
'</textarea>';
break;
default:
echo '<input type="text" name="'.$meta_box[ 'name' ].'" '.
'value="'.htmlspecialchars( $data[ $meta_box[ 'name' ] ] ).'">';
}
?>
但是我猜$data[$meta\u box['name']]
数组索引也不正确。为什么不遵循KISS规则,如果没有其他条件,请尝试下面的方法
<?php if( $meta_box['type'] === 'textarea' ) { ?>
<textarea name="<?php echo $meta_box[ 'name' ];?>"><?php echo htmlspecialchars($data[$meta_box['name']]); ?></textarea>
<?php } else { ?>
<input type="text" name="<?php echo $meta_box['name']; ?>"
value="<?php echo htmlspecialchars( $data[$meta_box['name']]); ?>" /> <?php }?>
为什么你不遵循接吻规则,如果你没有其他条件,试试下面的方法
<?php if( $meta_box['type'] === 'textarea' ) { ?>
<textarea name="<?php echo $meta_box[ 'name' ];?>"><?php echo htmlspecialchars($data[$meta_box['name']]); ?></textarea>
<?php } else { ?>
<input type="text" name="<?php echo $meta_box['name']; ?>"
value="<?php echo htmlspecialchars( $data[$meta_box['name']]); ?>" /> <?php }?>
有很多方法可以逃避内容:
$a = 5;
$b = array(3);
// Single quotes treats the insides as you type it, except for a few like
// double-backslash (which makes a backslash) or backslash apostrophe, which
// allows a single apostrophe, so including a dollar sign inside will actually
// print it out to screen
$content = 'This will print a literal dollar sign followed by an "a": $a'; // $a
$content = 'This will print a backslash and a literal dollar sign followed by an "a": \$a'; // \$a
// However, variables inside double quotes get resolved with their variable values,
// unless you have preceded the dollar sign with a backslash to let it be treated
// as a literal dollar
$content = "This will include the value of variable \$a, as it is looking for a variable: $a"; // 5
$content = "This will print a literal dollar sign followed by an \"a\" because we have backslash-escaped it: \$a"; // $a
// For cases where you need to use brackets with your variable and you wish to transclude
// the content, you need to enclose the variable inside curly braces. This helps PHP know
// that the variable has actually ended, and that you didn't want to actually print the
// bracket afterward
$content = 'This will print a literal dollar sign followed by an "a" and enclosed in braces: {$a}'; // {$a}
$content = "This will include the value of variable \$a, as it is looking for a variable: {$a}"; // 5
$content = 'This will print a literal dollar sign followed by an "a" and brackets and enclosed in braces: {$b[0]}'; // {$b[0]}
$content = "This will include the value of variable \$a, as it is looking for a variable: {$b[0]}"; // 3
…在HTML(或XML,包括真正的XHTML)中,您还可以进行转义;无论HTML中有单引号还是双引号,都可以使用转义:
有助于在引号内使用引号:
<!-- Hexadecimal escape -->
<input type="text" value="I want a quote here: " " /> <!-- " -->
<!-- Decimal escape -->
<input type="text" value="I want a quote here: " " /> <!-- " -->
<!-- Predefined entity escape -->
<input type="text" value="I want a quote here: " " /> <!-- " -->
和CSS
p:before {content: '\27'} /* ' */
在JavaScript中,双引号或单引号具有相同的含义(除了需要反斜杠转义不同的字符)
在CSS中,这是相同的,但反斜杠是不同的,因为它们通常被期望后跟一个十六进制Unicode序列(或另一个反斜杠),否则将被忽略
在JavaScript中(与PHP中的双引号一样),反斜杠在转义其他序列(如“\n”或其他反斜杠)时可能具有特殊意义,但在其他情况下会被忽略
您可以找到这些用于查找任何字符的“Unicode代码点”,不仅仅是标点符号,还包括任何脚本中的任何字符。它们通常表示为U+00A0或U+0027(注意,也可以包括字母A-F)。很少情况下,它们可能需要6位数字(或2位4位数字的组合),但通常4位就足够了。有很多方法可以转义内容:
$a = 5;
$b = array(3);
// Single quotes treats the insides as you type it, except for a few like
// double-backslash (which makes a backslash) or backslash apostrophe, which
// allows a single apostrophe, so including a dollar sign inside will actually
// print it out to screen
$content = 'This will print a literal dollar sign followed by an "a": $a'; // $a
$content = 'This will print a backslash and a literal dollar sign followed by an "a": \$a'; // \$a
// However, variables inside double quotes get resolved with their variable values,
// unless you have preceded the dollar sign with a backslash to let it be treated
// as a literal dollar
$content = "This will include the value of variable \$a, as it is looking for a variable: $a"; // 5
$content = "This will print a literal dollar sign followed by an \"a\" because we have backslash-escaped it: \$a"; // $a
// For cases where you need to use brackets with your variable and you wish to transclude
// the content, you need to enclose the variable inside curly braces. This helps PHP know
// that the variable has actually ended, and that you didn't want to actually print the
// bracket afterward
$content = 'This will print a literal dollar sign followed by an "a" and enclosed in braces: {$a}'; // {$a}
$content = "This will include the value of variable \$a, as it is looking for a variable: {$a}"; // 5
$content = 'This will print a literal dollar sign followed by an "a" and brackets and enclosed in braces: {$b[0]}'; // {$b[0]}
$content = "This will include the value of variable \$a, as it is looking for a variable: {$b[0]}"; // 3
…在HTML(或XML,包括真正的XHTML)中,您还可以进行转义;无论HTML中有单引号还是双引号,都可以使用转义:
有助于在引号内使用引号:
<!-- Hexadecimal escape -->
<input type="text" value="I want a quote here: " " /> <!-- " -->
<!-- Decimal escape -->
<input type="text" value="I want a quote here: " " /> <!-- " -->
<!-- Predefined entity escape -->
<input type="text" value="I want a quote here: " " /> <!-- " -->
和CSS
p:before {content: '\27'} /* ' */
在JavaScript中,双引号或单引号具有相同的含义(除了需要反斜杠转义不同的字符)
在CSS中,这是相同的,但反斜杠是不同的,因为它们通常被期望后跟一个十六进制Unicode序列(或另一个反斜杠),否则将被忽略
在JavaScript中(与PHP中的双引号一样),反斜杠在转义其他序列(如“\n”或其他反斜杠)时可能具有特殊意义,但在其他情况下会被忽略
您可以找到这些用于查找任何字符的“Unicode代码点”,不仅仅是标点符号,还包括任何脚本中的任何字符。它们通常表示为U+00A0或U+0027(注意,也可以包括字母A-F)。很少情况下,它们可能需要6位数字(或2位4位数字的组合),但通常4位就足够了。您应该理解的问题不止一个1。如何使用<代码>一点:不要在<代码> <代码>之间放置空格。否则,你将在浏览器中获得数据空间。你应该考虑接受其中一个答案。你不应该只理解1个问题。如何使用<代码>一点:不要在<代码> <代码>之间放置空间。否则,你将在浏览器中获得数据空间。你应该考虑接受其中一个答案。我确信你的意思是反斜杠''而不是'/':“我肯定你的意思是反斜杠''而不是'/':谢谢,但是第一个和第二个代码(第51行,BREAK;
在哪里)让我得到了意想不到的中断。@alexchenco:你说得对。我修正了上面答案中的拼写错误。谢谢,但是第一个和第二个代码(第51行,BREAK;
)出现了意想不到的中断。@alexchenco:你说得对。我修正了上面答案中的错误。