KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using PHP preg_quote function to escape a string
PRODUCT: 4D | VERSION: 14.3 | PLATFORM: Mac & Win
Published On: April 10, 2015

There are many different ways to escape a string in 4D. Here is another way using php’s preg_quote to escape a string. This will add backslash (\) in front of the following characters: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

// Method: PHP_preg_quote
// Parameters
// $1 - string to escape
// $0 - escaped string
// ----------------------------------------------------

C_TEXT($0;$1;$result;$string)
C_BOOLEAN($err)

If (Count parameters>=1)
  $string:=$1
  $err:=PHP Execute("";"preg_quote";$result;$string)
  If ($err=True)
    $0:=$result
  Else
    $0:=$string
  End if
End if


Ex:
C_TEXT($string;$result)
$string:="test\\String+test.string"
$result:=PHP_preg_quote ($string)
//$result becomes: “test\\\\String\\+\\.string”