I'm not sure if this is a cleaner way of writing this, but I think I don't have problems here:
我不确定这是否是一种更清晰的写作方式,但我认为我这里没有问题:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="<?php echo $meta_box[ 'name' ]; ?>">
<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>
</textarea>'
break;
Here is more complicated, since the elements to be echoed have single and double quotes:
这里更复杂,因为要回显的元素有单引号和双引号:
default:
echo "<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>"
value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>"" /><?php
}
Any suggestions to rewrite this code?
有没有重写此代码的建议?
This is the full switch statement:
这是完整的switch语句:
<?php switch ( $meta_box['type'] ) {
case 'textarea':
echo '<textarea name="<?php echo $meta_box[ 'name' ]; ?>">
<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>
</textarea>'
break;
default:
echo "<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>"
value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>"" /><?php
}
}
?>
6 个解决方案
#1
2
If you really want to output PHP code:
如果你真的想输出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\' ] ] ) ?'.'>">';
?>
Otherwise this makes more sense to me:
否则这对我来说更有意义:
<?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' ] ] ).'">';
}
?>
But I guess that $data[$meta_box['name']]
array index isn't correct either.
但我猜$ data [$ meta_box ['name']]数组索引也不正确。
#2
2
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";
#3
1
Why u dont follow KISS rule, try below if u have no more conditions
为什么你不遵守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 }?>
Happy to help :)
乐于帮助 :)
#4
0
I tend to use double quotes for the echo and single quotes within that
我倾向于使用双引号来表示回声和单引号
echo "Hello string test ' ";
or you can use the escape \
或者你可以使用逃生
echo "hello string test \" ";
Also in your code you are doing an echo within an echo.
同样在你的代码中,你在回声中做回声。
#5
0
You can escape the quotiation makrs like this:
你可以像这样逃避引用makrs:
echo "<input type=\"text\" name=\"test\" />;
But why dont you make it this way:
但为什么不这样做:
echo '<textarea name="' . $meta_box[ 'name' ] . '">
#6
0
There are a lot of ways to escape content:
有很多方法可以逃避内容:
$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
...and in HTML (or XML, including true XHTML), you can also do escaping; you can use the escapes no matter whether there are single or double quotes in the HTML:
...在HTML(或XML,包括真正的XHTML)中,您也可以进行转义;无论HTML中是单引号还是双引号,都可以使用转义:
Helpful for quotation marks used inside quotation marks:
对引号内使用的引号有帮助:
<!-- 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: " " /> <!-- " -->
Helpful for apostrophes inside apostrophes:
撇号内的撇号有帮助:
<!-- Hexadecimal escape -->
<input type="text" value='I want an apostrophe here: ' ' /> <!-- ' -->
<!-- Decimal escape -->
<input type="text" value='I want an apostrophe here: ' ' /> <!-- '-->
<!-- The following sadly only works in XML, but not HTML;
on the other hand, XML doesn't haven't a lot of the convenient
entities like ` ` predefined though, but you can always use hex
or dec safely. -->
<input type="text" value="I want an apostrophe here: ' " /> <!-- ' -->
Notice that the hexadecimal tends to be more useful as it is also used in JavaScript:
请注意,十六进制往往更有用,因为它也在JavaScript中使用:
var content = '\u0027'; // '
and CSS
p:before {content: '\27'} /* ' */
In JavaScript, double-quotes or single quotes have the same meaning (except for fact you need to backslash-escape a different character).
在JavaScript中,双引号或单引号具有相同的含义(除了事实,您需要反斜杠 - 转义不同的字符)。
In CSS it is the same, but backslashes are different in that they are typically expected to be followed by a hex Unicode sequence (or another backslash) and are ignored otherwise.
在CSS中它是相同的,但反斜杠是不同的,因为它们通常预期后面是十六进制Unicode序列(或另一个反斜杠),否则将被忽略。
In JavaScript (as with double quotes in PHP), the backslash may have special meaning in escaping other sequences such as '\n' or another backslash, but is ignored otherwise.
在JavaScript中(与PHP中的双引号一样),反斜杠在转义其他序列(例如'\ n'或其他反斜杠)时可能具有特殊含义,但是否则会被忽略。
You can find these for looking up the "Unicode codepoint" for any character, not just for punctuation, but also for any character in any script. These are usually expressed like U+00A0 or U+0027 (note that it can include letters A-F too). Rarely, they can require 6 digits (or a combination of 2 four digit ones), but usually 4 is sufficient.
您可以找到这些用于查找任何字符的“Unicode代码点”,不仅用于标点符号,还用于任何脚本中的任何字符。这些通常表示为U + 00A0或U + 0027(注意它也可以包括字母A-F)。很少,他们可以要求6位数(或2位四位数的组合),但通常4就足够了。
#1
2
If you really want to output PHP code:
如果你真的想输出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\' ] ] ) ?'.'>">';
?>
Otherwise this makes more sense to me:
否则这对我来说更有意义:
<?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' ] ] ).'">';
}
?>
But I guess that $data[$meta_box['name']]
array index isn't correct either.
但我猜$ data [$ meta_box ['name']]数组索引也不正确。
#2
2
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";
#3
1
Why u dont follow KISS rule, try below if u have no more conditions
为什么你不遵守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 }?>
Happy to help :)
乐于帮助 :)
#4
0
I tend to use double quotes for the echo and single quotes within that
我倾向于使用双引号来表示回声和单引号
echo "Hello string test ' ";
or you can use the escape \
或者你可以使用逃生
echo "hello string test \" ";
Also in your code you are doing an echo within an echo.
同样在你的代码中,你在回声中做回声。
#5
0
You can escape the quotiation makrs like this:
你可以像这样逃避引用makrs:
echo "<input type=\"text\" name=\"test\" />;
But why dont you make it this way:
但为什么不这样做:
echo '<textarea name="' . $meta_box[ 'name' ] . '">
#6
0
There are a lot of ways to escape content:
有很多方法可以逃避内容:
$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
...and in HTML (or XML, including true XHTML), you can also do escaping; you can use the escapes no matter whether there are single or double quotes in the HTML:
...在HTML(或XML,包括真正的XHTML)中,您也可以进行转义;无论HTML中是单引号还是双引号,都可以使用转义:
Helpful for quotation marks used inside quotation marks:
对引号内使用的引号有帮助:
<!-- 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: " " /> <!-- " -->
Helpful for apostrophes inside apostrophes:
撇号内的撇号有帮助:
<!-- Hexadecimal escape -->
<input type="text" value='I want an apostrophe here: ' ' /> <!-- ' -->
<!-- Decimal escape -->
<input type="text" value='I want an apostrophe here: ' ' /> <!-- '-->
<!-- The following sadly only works in XML, but not HTML;
on the other hand, XML doesn't haven't a lot of the convenient
entities like ` ` predefined though, but you can always use hex
or dec safely. -->
<input type="text" value="I want an apostrophe here: ' " /> <!-- ' -->
Notice that the hexadecimal tends to be more useful as it is also used in JavaScript:
请注意,十六进制往往更有用,因为它也在JavaScript中使用:
var content = '\u0027'; // '
and CSS
p:before {content: '\27'} /* ' */
In JavaScript, double-quotes or single quotes have the same meaning (except for fact you need to backslash-escape a different character).
在JavaScript中,双引号或单引号具有相同的含义(除了事实,您需要反斜杠 - 转义不同的字符)。
In CSS it is the same, but backslashes are different in that they are typically expected to be followed by a hex Unicode sequence (or another backslash) and are ignored otherwise.
在CSS中它是相同的,但反斜杠是不同的,因为它们通常预期后面是十六进制Unicode序列(或另一个反斜杠),否则将被忽略。
In JavaScript (as with double quotes in PHP), the backslash may have special meaning in escaping other sequences such as '\n' or another backslash, but is ignored otherwise.
在JavaScript中(与PHP中的双引号一样),反斜杠在转义其他序列(例如'\ n'或其他反斜杠)时可能具有特殊含义,但是否则会被忽略。
You can find these for looking up the "Unicode codepoint" for any character, not just for punctuation, but also for any character in any script. These are usually expressed like U+00A0 or U+0027 (note that it can include letters A-F too). Rarely, they can require 6 digits (or a combination of 2 four digit ones), but usually 4 is sufficient.
您可以找到这些用于查找任何字符的“Unicode代码点”,不仅用于标点符号,还用于任何脚本中的任何字符。这些通常表示为U + 00A0或U + 0027(注意它也可以包括字母A-F)。很少,他们可以要求6位数(或2位四位数的组合),但通常4就足够了。