How to have apostrophes in PHP string?

江枫思渺然 提交于 2019-12-24 06:01:02

问题


Part of my PHP code includes the construction of a URL string as follows:

$msg = $_REQUEST["msg"];
$content =  'action=sendmsg'. 
                '&user='.rawurlencode($username). 
                '&password='.rawurlencode($password). 
                '&to='.rawurlencode($destination). 
                '&text='.rawurlencode($msg);

When $msg happens to contain an apostrophe, it get sent as "\'".

What do I need to do to make sure the backslash is not inserted by PHP?


回答1:


You probably want to check out stripslashes: http://php.net/manual/en/function.stripslashes.php




回答2:


Assume you want to send the $content variable instead of just stripping the backslashes, Consider to use urlencode() instead of rawurlencode(). Also, you can use the function once for your $content variable.

$content = urlencode($content);

UPDATE: both urlencode and rawurlencode may not fit your case. Why don't you just send out the $content without URL encode? How do you send our the query string? If you are using cURL, you do not need to encode the parameters.




回答3:


You can try

  1. Stipslashes
  2. or put the string in "" and add ' where you want.


来源:https://stackoverflow.com/questions/15331037/how-to-have-apostrophes-in-php-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!