Last active
September 21, 2020 11:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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