简体   繁体   中英

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. 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?!

-- edit --

Sorry, my mistake, it does work. I'd missed my close bracket after ['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

<?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 )

You must have a mis-matched ' somewhere earlier in your code. PHP will NOT treat a ; inside a string as a statement terminator. Since it is terminating the statement, it's not "inside" a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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