php 如何从字符串中删除双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12111887/
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 02:51:26  来源:igfitidea点击:

How to remove double quotes from a string

phpstring

提问by maget0ron

This is my string:

这是我的字符串:

$a='"some text';

$a='"some text';

How can I remove the double quote so my output will look like this?

如何删除双引号使我的输出看起来像这样?

some text

some text

回答by John Conde

str_replace()

str_replace()

echo str_replace('"', '', $a);

回答by Aman Garg

if string is: $str = '"World"';

如果字符串是: $str = '"World"';

ltrim()function will remove only first Double quote.

ltrim()函数将只删除第一个双引号。

Output: World"

输出: World"

So instead of using both these function you should use trim(). Example:

因此,您应该使用trim(). 例子:

$str = '"World"';
echo trim($str, '"');

Output-

输出-

World

回答by Wesley Murch

Probably makes the most sense to use ltrim()since str_replace()will remove all the inner quote characters (depends, maybe that's what you want to happen).

可能最有意义,ltrim()因为str_replace()将删除所有内部引号字符(取决于,也许这就是您想要发生的)。

ltrim— Strip whitespace (or other characters) from the beginning of a string

ltrim— 从字符串的开头去除空格(或其他字符)

echo ltrim($string, '"');

If you want to remove quotes from both sides, just use regular trim(), the second argument is a string that contains all the characters you want to trim.

如果要删除两边的引号,只需使用 regular trim(),第二个参数是一个字符串,其中包含要修剪的所有字符。

回答by Asciiom

Use str_replace

使用str_replace

$a = str_replace('"', '', $a);

回答by Sharma Vikram

There are different functions are available for replacing characters from string below are some example

有不同的函数可用于替换字符串中的字符,下面是一些示例

    $a='"some text';
    echo 'String Replace Function<br>';
    echo 'O/P : ';
    echo $rs =str_replace('"','',$a);
    echo '<br>===================<br>';
    echo 'Preg Replace Function<br>';
    echo 'O/P : ';  
    echo preg_replace('/"/','',$a);
    echo '<br>===================<br>';
    echo 'Left Trim Function<br>';
    echo 'O/P : ';  
    echo ltrim($a, '"');
    echo '<br>===================';

Here is the O/P

这是 O/P

Output image

输出图像

回答by user5544327

You can do this:

你可以这样做:

str_replace()
echo str_replace('\"', '', $a);