What are best practices for handling quotation marks and brackets when extracting values from a string in PHP?

When extracting values from a string in PHP that contains quotation marks or brackets, it is important to properly handle these characters to avoid syntax errors or unexpected results. One way to do this is by using functions like `addslashes()` to escape quotation marks and `preg_replace()` to remove brackets before extracting the desired value.

$string = 'This is a "sample" [string] with values inside brackets';
$escapedString = addslashes($string);
$cleanedString = preg_replace('/\[.*?\]/', '', $escapedString);
$extractedValue = substr($cleanedString, strpos($cleanedString, '"') + 1, strpos($cleanedString, '"', strpos($cleanedString, '"') + 1) - strpos($cleanedString, '"') - 1);

echo $extractedValue;