简体   繁体   English

PHP 中的转义分号?

[英]Escape Semicolon in PHP?

I've created a database where I have product attributes separated with semicolons ;我创建了一个数据库,其中产品属性用分号分隔; . . The reason I chose this is it's incredibly unlikely come up naturally as part of one of the specifications.我选择它的原因是它不太可能自然地作为规范之一的一部分出现。

I was wishing to do this:我希望这样做:

<ul>
  <li><?php str_replace(';','</li><li>',$row['specifications']); ?></li>
</ul>

It would have created a nice unordered list.它会创建一个很好的无序列表。 However, the semicolon is seen as part of the PHP code itself rather than part of the str_replace function.但是,分号被视为 PHP 代码本身的一部分,而不是str_replace function 的一部分。 It's not being escaped with the normal backslash method.它没有被正常的反斜杠方法转义。

Is there any way of escaping the semicolon character in this function or have I just been a bit daft in choosing the semicolon and should've chosen something else?!有没有 escaping 这个 function 中的分号字符的任何方式,或者我只是在选择分号时有点愚蠢,应该选择别的东西?!

-- edit -- - 编辑 -

Sorry, my mistake, it does work.对不起,我的错误,它确实有效。 I'd missed my close bracket after ['specifications'].在 ['specifications'] 之后,我错过了我的右括号。

Thanks for the answers, though!不过,感谢您的回答!

$test = "asd;123;asd;123";
echo '<ul>';
echo '<li>'.str_replace(';','</li><li>',$test).'</li>';
echo '</ul>';

Works fine for me.对我来说很好。

I'm not pretty sure what are you trying to achieve, but your snippet is wrong我不太确定您要达到什么目的,但是您的代码段是错误的

http://sandbox.phpcode.eu/g/d92eb.php http://sandbox.phpcode.eu/g/d92eb.php

<?php 
$row['specifications'] = 't;e;s;t'; 
?> 
<ul> 
  <li><?php echo str_replace(';','</li><li>',$row['specifications']); ?></li> 
</ul>

is correct one (you have missed echo )是正确的(你错过了echo

You must have a mis-matched ' somewhere earlier in your code.您的代码中前面的某处必须有一个不匹配' PHP will NOT treat a ; PHP 不会处理; inside a string as a statement terminator.在字符串中作为语句终止符。 Since it is terminating the statement, it's not "inside" a string.由于它正在终止语句,因此它不在字符串“内部”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM