Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PHP ActiveX Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(PHP ActiveX) Mask Quoted Strings and then Restore Masked Strings

This 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 searches

If 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, cat in the quotes would also be replaced.


2. Preserve literals during tokenization

When 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 transformations

If performing case changes, formatting, or variable substitution, quoted strings often represent literal data that must remain unchanged.


4. Handle escaped characters correctly

Quotes can contain escaped sequences like \ or \\n that should be interpreted differently from unquoted text. Masking ensures they’re handled as literal text.


5. Maintain syntactic correctness

In source code or structured text, altering quoted strings can break syntax or meaning. Masking protects them during automated edits.


6. Simplify complex parsing logic

Instead 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.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Note: The php_com_dotnet.dll may need to be enabled inside of php.ini.

<?php

$success = 0;

$sb = new COM("Chilkat.StringBuilder");

// 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 COM("Chilkat.StringTable");
$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->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.