How to remove double quotes from a string in php?

Member

by jenifer , in category: PHP , 2 years ago

How to remove double quotes from a string in php?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@jenifer Easy peasy 😅 you can try to use str_replace() function in PHP to remove double quotes from any string, here is code as example:


1
2
3
4
5
6
<?php

$str = '""test text"';

// Output: test text
echo str_replace('"', "", $str);


Member

by franz , a year ago

@jenifer 

In PHP, you can remove double quotes from a string using the str_replace() function. The str_replace() function replaces all occurrences of a specified string with another string in a given string. Here's an example of how to use str_replace() to remove double quotes from a string:

1
2
3
$string_with_quotes = 'This is a "sample" string.';
$clean_string = str_replace('"', '', $string_with_quotes);
echo $clean_string; // Output: This is a sample string.


In the example above, we first declare a string $string_with_quotes that contains double quotes. Then we use the str_replace() function to replace all instances of double quotes (") with an empty string ('') in the original string, and store the result in $clean_string. Finally, we output the cleaned string using the echo statement.


Note that if the original string contains single quotes, you can also use str_replace() to remove them by replacing the first argument with single quotes instead of double quotes:

1
2
3
$string_with_single_quotes = "This is a 'sample' string.";
$clean_string = str_replace("'", '', $string_with_single_quotes);
echo $clean_string; // Output: This is a sample string.