简体   繁体   English

str_replace PHP不起作用

[英]str_replace PHP don't work

i'm writing this code: 我正在编写此代码:

$response = "PARCHEGGIO BASILE (Cap.) – Via E. Basile – Cavalcavia Brasa - V.le Regione Siciliana - V. Villagrazia (Barone Scala) – V. Emily Balch - V. Villagrazia - V. dell’Usignolo - V. dell’Allodola - V.le Regione Siciliana - VIA DEL LEVRIERE (Bonagia) (capolinea di transito) - V.dell’ Antilope - V. dell’ Ermellino - V.le Regione Siciliana - V. dell’Allodola - V. dell’Usignolo - V. Villagrazia - V.le Regione Siciliana – svincolo Brasa - Via E. Basile - PARCHEGGIO BASILE (Cap.)";
$response =  str_replace( " – " , "@" , $response );
echo $response;

But the result is: 但是结果是:

PARCHEGGIO BASILE (Cap.)@Via E. Basile@Cavalcavia Brasa - V.le Regione Siciliana - V. Villagrazia (Barone Scala)@V. Emily Balch - V. Villagrazia - V. dell’Usignolo - V. dell’Allodola - V.le Regione Siciliana - VIA DEL LEVRIERE (Bonagia) (capolinea di transito) - V.dell’ Antilope - V. dell’ Ermellino - V.le Regione Siciliana - V. dell’Allodola - V. dell’Usignolo - V. Villagrazia - V.le Regione Siciliana@svincolo Brasa - Via E. Basile - PARCHEGGIO BASILE (Cap.)

then, the str_replace don't function.. where i'm wrong? 然后,str_replace不起作用..我错了吗?

They are different characters, that have 0x2d and 0xe28093 (encoded as UTF-8) codes accordingly. 它们是不同的字符,分别具有0x2d0xe28093 (编码为UTF-8)代码。

If you look carefully you can even see that they are of different length. 如果仔细看,您甚至可以看到它们的长度不同。

So, php just does what you instructed it to: it tries to replace a substring, and as soon as there is no entry in the source string - it lefts it untouched. 因此,php只是按照您的指示进行操作:它尝试替换子字符串,并且一旦源字符串中没有条目,它就保持不变。

To replace all - you just use - character in your str_replace call instead of 要替换所有-您只需在str_replace调用中使用-字符,而不是

Useful links: 有用的链接:

Your sting was ill formatted, use this str_replace instead 您的字符串格式错误,请改用此str_replace

$response = str_replace([' – ', ' - '], "@", $response);

Because the previous string included the 0x2d or as well as 0xe28093 or - , different unicode characters. 因为前面的字符串包含0x2d以及0xe28093- ,所以不同的unicode字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM