1

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

#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 ...但我注意到 for ex int i = 12; 改为 int i = \n; 我不会发生什么;<

4

2 回答 2

2

使用str_ireplace

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

演示

于 2013-01-30T11:37:03.713 回答
1

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

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

AFAIK,这应该可以解决问题,并且应该避免您意外更换任何东西。
请参阅此处的文档

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

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

文档
另一个链接,另一个问题,也许值得一看

于 2013-01-30T11:33:48.740 回答