Paul Joshua Beldia Paul Joshua Beldia - 1 year ago 115
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 Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download