PHP - 摆脱卷曲撇号 [英] PHP - Getting rid of curly apostrophes

查看:106
本文介绍了PHP - 摆脱卷曲撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摆脱卷曲的撇号(我想象中从某种富文本文件中粘贴),我似乎正在打一个路障。以下代码对我来说不起作用。

  $ word =今天; 
$ search = array('„','“','’');
$ replace = array('','','');
$ word = str_replace($ search,$ replace,htmlentities($ word,ENT_QUOTES));

我最终得到的是$ word包含'今天’ s'。

当我从$ search数组中删除&符号时,替换会发生,但这显然会并没有完成工作,因为&符号留在字符串中。为什么str_replace遇到和符号失败?

解决方案

为什么不这样做:

  $ word = htmlentities(str_replace($ search,$ replace,$ word),ENT_QUOTES); 


I'm trying to get rid of curly apostrophes (ones pasted from some sort of rich text doc, I imagine) and I seem to be hitting a road block. The code below isn't working for me.

$word = "Today’s";
$search = array('„', '“', '’');
$replace = array('"', '"', "'");
$word = str_replace($search, $replace, htmlentities($word, ENT_QUOTES));

What I end up with is $word containing 'Today’s'.

When I remove the ampersands from my $search array, the replace takes place but this, obviously, will not get the job done since the ampersand is left in the string. Why is str_replace failing when it comes across the ampersands?

解决方案

Why not just do this:

$word = htmlentities(str_replace($search, $replace, $word), ENT_QUOTES);

?

这篇关于PHP - 摆脱卷曲撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆