Paul Joshua Beldia Paul Joshua Beldia - 1 month ago 7
PHP Question

PHP, Remove a string inside double quotation mark using preg_replace?

I have a PHP string of the following format:

$str = 'This is a "sample string"';


I want to remove the double quoted string. Like this:

This is a


What I tried:

$fileread = preg_replace('!/\*.*?\*/!s',' -', $fileread);
$fileread = preg_replace('![ \t]*//.*[ \t]*[\r\n]!', '', $fileread);
$separator = preg_replace('/"[^"]+"/','',$fileread);
$separator = explode(" ",$separator);


Is there an easier way to extract that?

Answer

You can simply match a string contained inside a double quotes and replace it with an empty string:

$str = 'This is a "sample string".';

$str = preg_replace('/".*"/', '', $str);

echo $str;