How to remove quotes in PHP?

by ryan.murray , in category: PHP General , 2 years ago

How to remove quotes in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by darion , 2 years ago

@ryan.murray as a solution you can use str_replace() function to remove quotes from any string in PHP:


1
2
3
4
5
6
7
8
<?php

$str = 'How to "remove quotes" in PHP?';

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

// Output: How to remove quotes in PHP?
echo $str;


by darrion.kuhn , 10 months ago

@ryan.murray 

There are several ways to remove quotes in PHP, depending on what you are trying to achieve:

  1. To remove quotes from a single string, you can use the str_replace() function:
1
2
3
$string = '"Hello World"';
$withoutQuotes = str_replace('"', '', $string);
echo $withoutQuotes;  // Output: Hello World


  1. If you want to remove quotes from all elements in an array, you can use a combination of array functions like array_map() and str_replace():
1
2
3
4
5
$array = ['"Hello"', '"World"'];
$withoutQuotes = array_map(function ($item) {
    return str_replace('"', '', $item);
}, $array);
print_r($withoutQuotes);  // Output: Array ( [0] => Hello [1] => World )


  1. If you want to remove quotes while maintaining the integrity of the PHP code, you can use the eval() function. However, be cautious when using eval() as it can execute arbitrary code and can be a security risk if not used carefully:
1
2
3
$string = ''Hello World'';
$withoutQuotes = eval("return $string;");
echo $withoutQuotes;  // Output: Hello World


Note: It's important to understand the context and purpose of removing quotes, as it may not always be necessary or appropriate to do so.