Skip to content

Instantly share code, notes, and snippets.

@DenesKellner
Last active September 21, 2020 11:48
Show Gist options
  • Save DenesKellner/6ceba817a140ef68007c1a206a6cc961 to your computer and use it in GitHub Desktop.
Save DenesKellner/6ceba817a140ef68007c1a206a6cc961 to your computer and use it in GitHub Desktop.
A string helper that replaces in-quote characters with a neutral one. It's good for handling things like SQL commands where quoted parts can contain keywords but you're not interested in them. Very simple tool, use with care.
<?php
function deleteQuotedParts($s,$replaceWith="#") {
$inQuote = "";
$protect = 0;
$sl = strlen($s);
$quoteChars = ["'"=>1,'"'=>1,"`"=>1];
for($i=0;$i<$sl;++$i) {
if($protect) {
if($inQuote) $s[$i] = $replaceWith;
$protect=0;continue;
}
$ch = $s[$i] ?: " ";
$qc = isset($quoteChars[$ch]);
if(!$inQuote) {if($qc) $inQuote=$ch;continue;}
if($ch==$inQuote) {$inQuote="";continue;}
if($ch=="\\") $protect=1;
$s[$i] = $replaceWith;
}
return $s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment