Take the 2-minute tour ×

Is there a simple way to escape/unescape an arbitrary character in PHP by doubling it, for example:

  • in ANSI SQL, "you can ""escape"" this way"
  • in printf(), you can express a %% this way

Escaping is quite easy with str_replace(), but reversing the process to unescape the string is not that easy, so does anyone know of a pair of functions, or a library to do this?

The idea is that I need to serialize an array this way:

array('a','b', '~','c') => 'a~b~~~~c'

The individual strings cannot be empty.

Update: just realized that this "encoding" is broken, thanks to the useful comments below.

share|improve this question
How many characters you have to escape? –  hakre Jun 22 '11 at 16:57
a~b~~~~c is ambiguous so you are asking the impossible. e.g., It could be two elements: a,b~~c. Why not just use serialize or json_encode? –  Matthew Jun 22 '11 at 17:08
'a~b~~~~c' This doesn't work. What about array('a', 'b~~c')? Why not use serialize() to serialize an array? –  phant0m Jun 22 '11 at 17:10
@konforce: I considered both options but they don't fit my precise need. About your comment, there is no ambiguity and the process is 100% reversible. A ~ not followed by another ~ is a delimiter, while ~~ is a normal ~ character. –  Benjamin Jun 22 '11 at 17:11
@Benjamin, array('a','b', '~','c') => 'a~b~~~~c'. array('a','b~~c') => 'a~b~~~~c'. Explain how it is not ambiguous to have two inputs that create the same output. –  Matthew Jun 22 '11 at 17:18
show 15 more comments

4 Answers

Seems to me that

str_replace($char, $char.$char, $string);


str_replace($char.$char, $char, $string);

would be the complements of each other.

share|improve this answer
I updated my question to put an example of what I need. I want to use $char as a delimiter to convert back to an array, but want $char.$char to be treated as a single $char and not as a two delimiters. –  Benjamin Jun 22 '11 at 17:02
Not sure you'd be able to make this work. You're dual purposing your escape character as both escape AND as separator. Given ~~~~ is it ,~,, ,,,,, ,,~, etc... –  Marc B Jun 22 '11 at 17:06
~~~~ is ~~, the same way %%%% is %% in printf() and """" is "" in an ANSI SQL identifier. –  Benjamin Jun 22 '11 at 17:13
sorry, I know what you mean now. I forgot to say that the individual strings cannot be empty, which I guess solves the issue? –  Benjamin Jun 22 '11 at 17:15
add comment
up vote 1 down vote accepted

As several users pointed it out in the comments, this question is flawed and has no possible answer.

A detailed explanation of my goal, and an appropriate answer have been provided at Provide a human-readable representation of an identifier?

share|improve this answer
add comment

Take a look at addcslashes() and stripcslashes which will allow you to slash those characters you want to. That's maybe helpful.

Next to that your question does not really say where exactly you see a problem in unescaping, so I can not offer anything else than using the str_replace in reverse.

share|improve this answer
I specifically don't want to use backslashes at all. I just updated my question with an example of what I need! –  Benjamin Jun 22 '11 at 17:04
add comment

Use preg_split() with


You have to use the flag PREG_SPLIT_DELIM_CAPTURE. After some struggling, I am quite surprised that there actually is a regex solution for this.



Array ( [0] => a [1] => b [2] => -- [3] => c [4] => hl--g [5] => ---- [6] => lo [7] => x ) 
share|improve this answer
You may have noticed that I use the delimiter "-". Found this a bit more convenient. :) In my regex you can replace "-" with any other character but remember to escape special regex characters with backslash. –  Leif Jun 22 '11 at 19:06
Thanks for your efforts, however as we exchanged on the question comments I realized that the problem itself was broken (see these comments for the reasons why). –  Benjamin Jun 22 '11 at 19:13
add comment

Your Answer


