php 从php字符串中删除奇怪的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1189007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
removing strange characters from php string
提问by mrpatg
this is what i have right now
这就是我现在所拥有的
Drawing an RSS feed into the php, the raw xml from the rss feed reads:
将 RSS 提要绘制到 php 中,来自 RSS 提要的原始 xml 内容如下:
Paul’s Confidence
The php that i have so far is this.
到目前为止,我拥有的 php 是这样的。
$newtitle = $item->title;
$newtitle = utf8_decode($newtitle);
The above returns;
以上返回;
Paul?s Confidence
If i remove the utf_decode, i get this
如果我删除 utf_decode,我会得到这个
Paula?s Confidence
When i try a str_replace;
当我尝试 str_replace 时;
$newtitle = str_replace("”", "", $newtitle);
It doesnt work, i get;
它不起作用,我明白了;
Paula?s Confidence
Any thoughts?
有什么想法吗?
采纳答案by czuk
Try this:
尝试这个:
$newtitle = html_entity_decode($newtitle, ENT_QUOTES, "UTF-8")
If this is not the solution browse this page http://us2.php.net/manual/en/function.html-entity-decode.php
如果这不是解决方案,请浏览此页面http://us2.php.net/manual/en/function.html-entity-decode.php
回答by David D
This is my function that always works, regardless of encoding:
无论编码如何,这是我始终有效的功能:
function RemoveBS($Str) {
$StrArr = str_split($Str); $NewStr = '';
foreach ($StrArr as $Char) {
$CharNo = ord($Char);
if ($CharNo == 163) { $NewStr .= $Char; continue; } // keep £
if ($CharNo > 31 && $CharNo < 127) {
$NewStr .= $Char;
}
}
return $NewStr;
}
How it works:
这个怎么运作:
echo RemoveBS('Hello ?how? ?are you??'); // Hello how are you?
回答by Keval Rathi
This will remove all non-ascii characters / special characters from a string.
这将从字符串中删除所有非 ascii 字符/特殊字符。
//Remove from a single line string
$output = "Likening a?not-criticala? with";
$output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
echo $output;
//Remove from a multi-line string
$output = "Likening a?not-criticala? with \n Likening a?not-criticala? with \r Likening a?not-criticala? with. ' ! -.";
$output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $output);
echo $output;
回答by mrpatg
I solved the problem. Seems to be a short fix rather than the larger issue, but it works.
我解决了这个问题。似乎是一个简短的修复而不是更大的问题,但它有效。
$newtitle = str_replace('a?', "'", $newtitle);
I also found this useful snippit that may help others with same problem;
我还发现了这个有用的片段,可以帮助其他有同样问题的人;
<?
$find[] = 'a?'; // left side double smart quote
$find[] = 'a'; // right side double smart quote
$find[] = 'a?'; // left side single smart quote
$find[] = 'a?'; // right side single smart quote
$find[] = 'a|'; // elipsis
$find[] = 'a”'; // em dash
$find[] = 'a“'; // en dash
$replace[] = '"';
$replace[] = '"';
$replace[] = "'";
$replace[] = "'";
$replace[] = "...";
$replace[] = "-";
$replace[] = "-";
$text = str_replace($find, $replace, $text);
?>
Thanks everyone for your time and consideration.
感谢大家的时间和考虑。
回答by FuzzyBits
Yeah this is not working for me. What is the workaround for this? – vaichidrewar Mar 12 at 22:29
是的,这对我不起作用。解决方法是什么?– vaichidrewar 3 月 12 日 22:29
Add this to the HTML head (or modify if already there):
将此添加到 HTML 头部(或修改,如果已经存在):
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
This will encode the funny charslike "a?" into UTF-8 so that the str_replace()function will interpret them properly.
这将编码有趣的字符,如“a?” 转换为 UTF-8,以便str_replace()函数正确解释它们。
Or you can do this:
或者你可以这样做:
ini_set('default_charset', 'utf-8');
回答by kalaithendral
Use the below PHP code to remove
使用以下 PHP 代码删除
html_entity_decode(mb_convert_encoding(stripslashes($name), "HTML-ENTITIES", 'UTF-8'))
回答by codemonkey
Is the character encoding setting for your PHP server something other than UTF-8? If so, is there a reason or could it be changed to UTF-8? Though we don't store data in UTF-8 in our database, I've found that setting the webserver's character set to UTF-8 seems to help resolve character set issues.
您的 PHP 服务器的字符编码设置是否不是 UTF-8?如果是这样,是否有原因或可以将其更改为 UTF-8?尽管我们的数据库中没有以 UTF-8 格式存储数据,但我发现将网络服务器的字符集设置为 UTF-8 似乎有助于解决字符集问题。
I'd be interested in hearing others' opinions about this... whether I'm setting myself up for problems by setting webserver to UTF-8 while storing submitted data in Latin1 in our mysql database. I know there was a reason I chose Latin1 for the database but can't recall what it was. Interestingly, our current setup seems to allow for non-UTF-8 character entry and subsequent rendering... it seems that storing in Latin1 doesn't prevent subsequent decoding and display of all UTF-8 characters?
我很想听听其他人对此的看法……我是否通过将网络服务器设置为 UTF-8 来设置自己的问题,同时将提交的数据存储在我们的 mysql 数据库中的 Latin1 中。我知道我为数据库选择 Latin1 是有原因的,但不记得它是什么。有趣的是,我们当前的设置似乎允许非 UTF-8 字符输入和后续渲染......似乎在 Latin1 中存储不会阻止所有 UTF-8 字符的后续解码和显示?
回答by codemonkey
Read up on http://us.php.net/manual/en/function.html-entity-decode.php
阅读http://us.php.net/manual/en/function.html-entity-decode.php
That & symbol is a html code so you can easily decode it.
那个 & 符号是一个 html 代码,所以你可以很容易地解码它。
回答by lalo
It does not work You need to use $arr1 = str_split($str) then foreach and echo($arr1[$k]) This will show you exactly which characters are written into the string.
它不起作用您需要使用 $arr1 = str_split($str) 然后 foreach 和 echo($arr1[$k]) 这将显示您确切地将哪些字符写入字符串。
回答by Rizwan Siddiquee
Please Try this.
$find[] = '/“/' //'a?'; // left side double smart quote
$find[] = '/”/' //'a'; // right side double smart quote
$find[] = '/‘/' //'a?'; // left side single smart quote
$find[] = '/’/' //'a?'; // right side single smart quote
$find[] = '/ /' //'a|'; // elipsis
$find[] = '/‖/' //'a”'; // em dash
$find[] = '/–/' //'a“'; // en dash
$replace[] = '“' // '"';
$replace[] = '”' // '"';
$replace[] = '‘' // "'";
$replace[] = '’' // "'";
$replace[] = '⋯' // "...";
$replace[] = '—' // "-";
$replace[] = '–' // "-";
$text = str_replace($find, $replace, $text);

