【问题标题】:how to replace special chars in php如何替换php中的特殊字符
【发布时间】:2013-01-14 04:43:31
【问题描述】:

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

#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.诠释 i = 12;改为 int i = \n;我不会发生的事;

【问题讨论】:

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


【解决方案1】:

使用str_ireplace

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

DEMO.

【讨论】:

    【解决方案2】:

    您可以使用 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

    【讨论】:

    • 接受的答案对我来说失败了,您的第一个示例中的方法有效。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多