通过在PHP中将它们加倍来转义字符 [英] Escaping characters by doubling them in PHP

查看:201
本文介绍了通过在PHP中将它们加倍来转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方式来将PHP中的任意字符加倍,例如:在ANSI SQL中




  • print(),可以这样转义

  • %%这种方式



使用 str_replace(),但是反转进程来解除字符串不是那么容易,所以有人知道一对函数,还是一个图书馆呢?



想法是我需要以这种方式序列化数组:



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



单独的字符串不能为空



更新:只是意识到这个编码是坏的,谢谢

解决方案

正如几位用户在评论中指出的那样,这个问题是有缺陷的,没有可能的答案



详细说明我的目标,并在提供标识符的人类可读表示?


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.

解决方案

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?

这篇关于通过在PHP中将它们加倍来转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆