Skip to content

Instantly share code, notes, and snippets.

@aklump
Last active September 22, 2023 02:52
Show Gist options
  • Save aklump/3c4eb19896129267c4ab74650d7ed9f4 to your computer and use it in GitHub Desktop.
Save aklump/3c4eb19896129267c4ab74650d7ed9f4 to your computer and use it in GitHub Desktop.
Add backslash to double quotes in a string where not already present.
<?php
/**
* Add backslash to double quotes unless already present.
*/
class EscapeDoubleQuotes {
public function __invoke(string $query): string {
// This is four-backslashes because of preg--confusing--that's why this
// class exists to remove the confusion.
return preg_replace('/(?<!\\\\)"/', '\"', $query);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment