php 如何输出带双引号的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6168298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:33:06  来源:igfitidea点击:

How to output a string with a double quotation mark?

phpquotes

提问by Itai Sagi

I need to output a string, which is basically a java code:

我需要输出一个字符串,它基本上是一个java代码:

I have something like this:

我有这样的事情:

$web = "...if (url.contains('.mp4'))..."

I need that the single quotation mark, will be a double one, and not in html code.

我需要单引号,将是双引号,而不是在 html 代码中。

Is it possible to do it?

有可能做到吗?

采纳答案by Tim Cooper

$new_str = str_replace('\'', '"', $web);

You could optional do it by modifying the actual string (note the use of \to escape the quotation marks):

您可以通过修改实际字符串来选择执行此操作(请注意使用\来转义引号):

$web = "...if (url.contains(\".mp4\"))..."

回答by mario

Short of doing a strtr()replacement, just escape your double quotes:

没有做strtr()替换,只需转义双引号:

$web = "...if (url.contains(\".mp4\"))..."

See http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.doublefor the complete list of escaping rules.

有关转义规则的完整列表,请参阅http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double

回答by Xuvi

You can use like this

你可以这样使用

$web = "...if (url.contains(\".mp4\"))...";

回答by Tarun Gupta

You should use this

你应该用这个

<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
?>

see http://docs.php.net/manual/en/function.htmlspecialchars.php

http://docs.php.net/manual/en/function.htmlspecialchars.php