如何在php中替换特殊字符

时间:2013-01-30 11:22:09

标签: php cakephp special-characters chars before-save

我有一个代码,我必须替换几个特殊的字符: 代码如下:

#include <iostream>\012#include <stdio.h>\012#include <stdlib.h>\012#include <limits>\012#include <string.h>\012using namespace std;\012\012void inclean(){\012\011std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n');\012}\012\012void die(string e){\012\011//cout << e;\012\011exit(1);\012}\012\012typedef struct {\012\011bool zajente;\012\011int rozmiar;\012} pudelko;\012\012int main() {\012\011int i = 12;\012setvbuf(stdout, NULL, _IONBF, 0);\012\011setvbuf(stderr, NULL, _IONBF, 0);\012\011int a;\012\011int lamount=0;\012\011cin >> a;\012\011if(!(a>=1&&a<=1000000)){die("Liczba mo\277e wynosi\346 od 1 do 1000000(10^6)");}\012\011pudelko  *pudelka=new pudelko[a];\012\011memset (pudelka,0,a*sizeof(pudelko));\012\011string b;\012\011inclean();\012\011getline(cin,b);\012\011int bl=b.length();\012\011for(int i=0;i<=bl;i++){\012\011\011char c=b.c_str()[i];\012\011\011if (c>=48&&c<=57){\012\011\011\011pudelka[lamount].rozmiar*=10;\012\011\011\011pudelka[lamount].rozmiar+=(int)c-48;\012\011\011\011pudelka[lamount].zajente=false;\012\011\011} else {\012\011\011\011if(!(pudelka[lamount].rozmiar>=1&&pudelka[lamount].rozmiar<=1000000000)) die("Liczba mo\277e wynosi\346 od 1 do 1000000000(10^9)");\012\011\011\011lamount++;\012\011\011\011if(lamount>=a) break;\012\011\011}\012\011}\012\011int cpa=lamount;\012\011int e=0;\012\011while (true){\012\011\011for(int z=e;z<=cpa;z++)\012\011\011\011if(pudelka[z].rozmiar>pudelka[e].rozmiar&&!pudelka[z].zajente){\012\011\011\011\011pudelka[z].zajente=true;\012\011\011\011\011lamount--; break;\012\011\011\011}\012\011\011if(e>=cpa){ cout << "" << lamount+1; break; }\012\011\011e++;\012\011\011}\012\011exit(0);\012\011return(0);\012}\012

我试图删除\ 012和\ 011是

 $this->data['StoredFile']['data'] = str_replace("\012","\n",$this->data['StoredFile']['data']);
 $this->data['StoredFile']['data'] = str_replace("\011","\t",$this->data['StoredFile']['data']);

也用过

$this->data['StoredFile']['data'] = stripslashes($this->data['StoredFile']['data']);

wchich删除\ 0然后通过str_replace我删除了11和12 ......但是我注意到了e.x. int i = 12;被改为int i = \ n;什么我不会发生;&lt;

2 个答案:

答案 0 :(得分:2)

使用str_ireplace

$output = str_ireplace (array('\012','\011'),array("\n","\t"), $string);

DEMO

答案 1 :(得分:1)

您可以使用chr函数将ascii字符替换为您需要或想要的任何字符/字符串:

str_replace(array(chr(012), chr(011)), array("\n","\t"), $string);

AFAIK,应该做的伎俩,应该避免你意外更换任何东西。
请参阅the docs here

话虽如此,输入字符串中可能会有更多的ascii字符,因此iconv函数可能更合适:

echo iconv('ASCII', 'UTF-8//IGNORE', $string);

The docs
另一个问题是,perhaps worth a look