Namiastka
Namiastka

Reputation: 74

how to replace special chars in php

I have a code that i have to replace few special chars: code below:

#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

what i tried to remove \012 and \011 was

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

Also used

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

wchich removed \0 and then by str_replace i removed 11 and 12... but i noticed that for e.x. int i = 12; was changed to int i = \n; what i dont wont to happen ;<

Upvotes: 1

Views: 1994

Answers (2)

Rikesh
Rikesh

Reputation: 26441

Use str_ireplace.

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

DEMO.

Upvotes: 2

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76413

You can use the chr function to replace ascii characters with whatever char/string you need or want:

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

AFAIK, that should do the trick, and should avoid your replacing anything by accident.
See the docs here.

That being said, there might be more ascii chars in your input string, so perhaps the iconv function is a better fit:

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

The docs.
Another link, to another question SO, perhaps worth a look

Upvotes: 1

Related Questions