![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PHP Extension) Mask Quoted Strings and then Restore Masked StringsThis example demonstrates how to mask the contents of single and/or double-quoted strings found in text, and then restore the masked strings.
Masking the contents of quoted strings before processing text is often necessary because the quoted portions can interfere with parsing, searching, or transforming the surrounding text. Here are common reasons: 1. Prevent false matches in searchesIf you run a pattern match (e.g., regular expression) for a word or symbol, you might unintentionally match inside quoted strings where it’s not meant to apply. Example: Search: replace "cat" → "dog" Text: He said "the cat sat" Without masking, 2. Preserve literals during tokenizationWhen splitting text into tokens (e.g., programming language source, CSV data), quoted strings may contain spaces, commas, or special characters that should not be treated as delimiters. 3. Avoid accidental transformationsIf performing case changes, formatting, or variable substitution, quoted strings often represent literal data that must remain unchanged. 4. Handle escaped characters correctlyQuotes can contain escaped sequences like 5. Maintain syntactic correctnessIn source code or structured text, altering quoted strings can break syntax or meaning. Masking protects them during automated edits. 6. Simplify complex parsing logicInstead of writing parsing rules that constantly check “am I inside quotes?”, masking them first allows you to process the rest of the text without quote-handling logic getting in the way. Note: This example requires Chilkat v11.2.0 or greater.
<?php include("chilkat.php"); $success = false; $sb = new CkStringBuilder(); // Here we have a string with some parts within single and double quotes. $s = 'This is \'quoted text\', empty string \'\', and 'another quoted string', non-usascii \'é\', yada \' yada yada.'; $sb->Append($s); print 'Original text: ' . $sb->getAsString() . "\n"; // Original text: This is "quoted text", empty string "", and 'another quoted string', non-usascii "é", yada " yada yada. // The MaskQuotedStrings method will mask quoted strings and store the contents of each masked string in a Chilkat StringTable. // We can restore the masked strings by calling RestoreMaskedStrings. // The quoteType can have the value 0, 1, or 2. // 0: Mask both single-quoted and double-quoted strings. // 1: Mask only single-quoted strings. // 2: Mask only double-quoted strings. $quoteType = 0; // Mask with the "_" char. $st = new CkStringTable(); $sb->MaskQuotedStrings('_',$quoteType,$st); // The quoted strings are now masked print $sb->getAsString() . "\n"; // Result: // This is "___________", empty string "", and '_____________________', non-usascii "__", yada " yada yada. // A few notes: // // 1) Empty quoted strings are ignored and won't have an entry in the output StringTable. // 2) Non-us-ascii chars within double-quotes will increase length. This is because the utf-8 byte represention of the string is masked. // In this example, the é char is represented by 2 bytes in utf-8, and thus the us-ascii mask char occupies each byte. // (Only us-ascii printable chars can be used for the mask.) // 3) Unclosed quotes are ignored. In this example, there is a final double-quote that is unclosed. // The output StringTable holds the contents of each masked string: $count = $st->get_Count(); $i = 0; while ($i < $count) { print $i . ': ' . $st->stringAt($i) . "\n"; $i = $i + 1; } // Output: // 0: quoted text // 1: another quoted string // 2: é // Restore the masked strings: $sb->RestoreMaskedStrings($quoteType,$st); print 'Restored: ' . $sb->getAsString() . "\n"; // Restored: This is "quoted text", empty string "", and 'another quoted string', non-usascii "é", yada " yada yada. ?> |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.