3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "To be escaped: + - = && || > < ! ( ) { } [ ] ^ \" ~ * ? : \ / triple ||| and split '&<&'"; print_r( [ $string, LouisBarranqueiro($string), MikkoRantalainen($string), mickmackusa($string) ] ); function LouisBarranqueiro($string) { $regex = "/[\\+\\-\\=\\&\\|\\!\\(\\)\\{\\}\\[\\]\\^\\\"\\~\\*\\<\\>\\?\\:\\\\\\/]/"; $string = preg_replace_callback ($regex, function ($matches) { return "\\" . $matches[0]; }, $string); return $string; } function MikkoRantalainen($s) { static $replacements = array(); if (!$replacements) { # https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters $replacements = array( "\\" => "\\\\", # must be done first to not double encode later backslashes! "+" => "\\+", "-" => "\\-", "=" => "\\=", "&" => "\\&", "|" => "\\|", ">" => "", # cannot be safely encoded "<" => "", # cannot be safely encoded "!" => "\\!", "(" => "\\(", ")" => "\\)", "{" => "\\{", "}" => "\\}", "[" => "\\[", "]" => "\\]", "^" => "\\^", "\"" => "\\\"", "~" => "\\~", "*" => "\\*", "?" => "\\?", ":" => "\\:", "/" => "\\/", ); } return str_replace(array_keys($replacements), array_values($replacements), $s); } function mickmackusa($string) { return preg_replace( [ '_[<>]+_', // prevent hack where double-symbol is split by gt/lt symbol '_[-+=!(){}[\]^"~*?:\\/\\\\]|&(?=&)|\|(?=\|)_', ], [ '', '\\\\$0', ], $string ); }
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => To be escaped: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ / triple ||| and split '&<&' [1] => To be escaped\: \+ \- \= \&\& \|\| \> \< \! \( \) \{ \} \[ \] \^ \" \~ \* \? \: \\ \/ triple \|\|\| and split '\&\<\&' [2] => To be escaped\: \+ \- \= \&\& \|\| \! \( \) \{ \} \[ \] \^ \" \~ \* \? \: \\ \/ triple \|\|\| and split '\&\&' [3] => To be escaped\: \+ \- \= \&& \|| \! \( \) \{ \} \[ \] \^ \" \~ \* \? \: \\ \/ triple \|\|| and split '\&&' )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
38.16 ms | 406 KiB | 5 Q