str_replace 不替换撇号 [英] str_replace not replacing apostrophes

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

问题描述

我在这里阅读了一个不同的问题,它解决了同样的问题,但我似乎无法弄清楚这里发生了什么.我正在尝试用 str_replace 替换我代码中的撇号,但它不起作用.我有一个字符串要测试:

I've read a different question on here that addresses this same problem and I can't seem to figure out what's going on here. I'm trying replace an apostrophe in my code with str_replace and it's not working. I've got a string to test:

$clue_question = "If you’re rowin’ the Rhone from start to finish, you begin in this mountain range";

然后一些字符串替换及其结果:

And then some string replaces and their results:

$new_string = str_replace("I", "a", $clue_question)."<br />";
//af you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$another_new_string = str_replace("'", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$yet_another_new_string = str_replace("&#039;", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$sdf_another_new_string = str_replace("&#096;", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$sdf_another_new_string_sdf = str_replace("&#239;", "VB", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$yet_another = str_replace("’", "sdgd", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

$yet_another = str_replace("\’", "sdgd", $clue_question)."<br />";
//If you’re rowin’ the Rhone from start to finish, you begin in this mountain range

我试过 ASCII 等价物,我试过转义它们,我试过它们正常.我不知道为什么我的字符串中没有替换单个撇号(或者可能是单引号).我的语法很好,因为第一个 str_replace() 函数工作正常.我在谷歌上搜索过,我唯一能想到的就是我在这里检查了另一个问题后的 PHP 版本.我运行了 phpinfo() 并且我的版本是 5.2.17,所以我假设这不是问题.

I've tried the ASCII equivalents, I've tried escaping them, I've tried them normal. I have no idea why the single apostrophe (or perhaps it's a single quote) is not being replaced in my string. My syntax is fine because the first str_replace() function worked correctly. I've Googled around and the only thing I could think was it was my PHP version after checking another question on here. I ran phpinfo() and I have version 5.2.17 so I'm assuming that's not the issue.

有什么想法吗?谢谢.

抱歉,我最初有一个类型,并且初始变量为 $question 而不是 $clue_question.此情况并非如此.我能够打印出屏幕上的字符串并正确设置,这只是没有格式化.感谢您迄今为止在这个问题上的活动!

My apologies, I originally had a type and had the initial variable as $question instead of $clue_question. This was not the case. I'm able to print out the strings on the screen and have that set correctly, it's just not formatting. Thanks for the activity so far on this question!

推荐答案

请注意,&#039; 等是 HTMLisms,用于在某些东西中表达特定于字符集的图形的方法纯文本.要让 str_replace() 理解 &#039;,您需要将 HTML 转换为真正的字符集.

Note that &#039; and the like are HTMLisms, methods for expressing character-set-specific figures in something that is text-only. For str_replace() to understand &#039;, you would need to convert the HTML to a real character set.

我真的不知道为什么你的非 HTML 尝试失败了,但是如果你的输入数据是用你不想处理的字符集编码的,那么你可能需要转换成更容易管理的东西.

I can't really tell why your non-HTML attempts are failing, but if your input data are encoded in a character set you don't want to handle, then perhaps you need to convert to something more easily managed.

<?php

$q = "If you’re rowin’ the Rhone\n";

print "Original:  " . $q;
print "Converted: " . iconv("UTF-8","ASCII//TRANSLIT",$q);

对我来说,在无法正确显示 UTF-8 的 xterm 中,这会得到以下结果:

For me, in an xterm that doesn't properly display UTF-8, this gets me the following result:

Original:  If youâre rowinâ the Rhone
Converted: If you're rowin' the Rhone

如果您可以将输入降级为始终按预期运行的内容,那么您可能会更轻松地操作它.

If you can get away with downgrading your input to stuff that always behaves as expected, you may have an easier time manipulating it.

请注意,这不是处理特殊字符的通用解决方案.使用 iconv() 来限制你的字符集可能会产生意想不到的后果,比如删除你不知道的特殊字符.小心轻放.彻底测试.上完厕所后一定要洗手.

Note that this is NOT a general solution for handling special characters. Using iconv() to limit your character set may have unintended consequences like removing characters you didn't know were special. Handle with care. Test thoroughly. Always wash your hands after using the bathroom.

这篇关于str_replace 不替换撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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