简体   繁体   English

php,如何将特殊字符转换为文本?

[英]php, how to convert special characters to text?

xxxi have the_title() that returns some text, in this case Blue & Whiny xxxi 有返回一些文本的the_title() ,在本例中为Blue & Whiny Blue & Whiny

the problem as we can see is that the & character looks different我们可以看到的问题是 & 字符看起来不同

how do i turn Blue & Whiny我怎么变Blue & Whiny Blue & Whiny into Blue & Whiny i tryed: htmlspecialchars_decode(the_title()) , html_entity_decode(the_title()) , htmlspecialchars(the_title()) and nothing. Blue & WhinyBlue & Whiny我试过了: htmlspecialchars_decode(the_title()) , html_entity_decode(the_title()) , htmlspecialchars(the_title())什么都没有。

i want to convert &我想转换& to &&

there is not much code to share, I just do this: <?php the_title()?> and i get Blue &#038; Whiny没有太多代码可以分享,我只是这样做: <?php the_title()?>我得到Blue &#038; Whiny Blue &#038; Whiny . Blue &#038; Whiny If i use get_the_title() it wont display anything如果我使用get_the_title()它不会显示任何内容

Any ideas?有任何想法吗? Thanks谢谢

edit1.编辑1。 ill share some code:生病分享一些代码:

<script type="text/javascript">
function showShareUI() {

var act = new gigya.services.socialize.UserAction();
act.setUserMessage("Check out this article.");
act.setTitle("Trends on Explore Talent - <?php  the_title(); ?>");
act.setDescription("<?php  get_the_content();  ?>");
act.setLinkBack("<?php  the_permalink();  ?>");
act.addActionLink("Check out this article", "<?php the_permalink(); ?>");

var image = {
src: 'http://xxx.com/wp-content/uploads/2011/05/BOTTOM_BANNER.jpg',
href: '<?php the_permalink();?>',
type: 'image'
}
act.addMediaItem(image);

var params = 
{
userAction: act,  // The UserAction object enfolding the newsfeed data.                                           
onError: onError,  // onError method will be summoned if an error occurs. 
onSendDone: onSendDone // onError method will be summoned after 
,showEmailButton: true
    // Gigya finishes the publishing process.
};

gigya.services.socialize.showShareUI(conf, params);
}

function onError(event) {
alert('An error has occured' + ': ' + event.errorCode + '; ' + event.errorMessage);
}

function onSendDone(event)
{
document.getElementById('status').style.color = "green";
document.getElementById('status').innerHTML = 'The newsfeed has been posted to: ' +     event.providers;
}
</script>

I've tried everything.我什么都试过了。 This starts to annoy me...这开始让我很烦...

html_entity_decode() is the correct way to do it. html_entity_decode()是正确的方法。

html_entity_decode("Blue &#038; Whiny");

Will produce:将产生:

Blue & Whiny蓝与发牢骚

If it's not working, make sure you don't have another issue - such as passing a string to it that is double encoded, or running htmlentities() on the string again later.如果它不起作用,请确保您没有其他问题 - 例如将一个经过双重编码的字符串传递给它,或者稍后再次在该字符串上运行htmlentities()

Demo: http://codepad.org/BHXGWXJi演示: http://codepad.org/BHXGWXJi

Double check with a literal string and var_dump() the output, you should see the decoded version.使用文字字符串和var_dump()仔细检查 output,您应该会看到解码后的版本。 Then var_dump(the_title()) , to make sure you are actually passing what you think you are to html_entity_decode() .然后var_dump(the_title()) ,以确保您实际上将您认为的内容传递给html_entity_decode()

html_entity_decode should do the trick. html_entity_decode应该可以解决问题。 If not, try to specify the third parameter $charset .如果没有,请尝试指定第三个参数$charset

Something like:就像是:

echo html_entity_decode(the_title(), ENT_QUOTES, 'UTF-8');

the_title() directly prints the title, so adding html_entity_decode() directly around that won't work. the_title()直接打印标题,因此直接在其周围添加 html_entity_decode( html_entity_decode()是行不通的。 You can, however, stop it from printing with its third function argument.但是,您可以使用其第三个 function 参数来阻止它打印。 Eg例如

<?php echo html_entity_decode(the_title('', '', false)) ?>

There's also get_the_title() , which doesn't directly print the title, but it requires the ID of the post you want the title of, in contrast with the_title , which prints the title of the current post in The Loop .还有get_the_title() ,它不直接打印标题,但它需要您想要标题的帖子的 ID,相比之下, the_title会在The Loop中打印当前帖子的标题。 So you need to do something like this:所以你需要做这样的事情:

<?php echo html_entity_decode(get_the_title($post->ID)) ?>

And actually, you should be able to simply do:实际上,您应该能够简单地执行以下操作:

<?php echo $post->post_title ?>

The only reason these utility functions are there is to escape things for you and add tags and stuff.这些实用程序功能存在的唯一原因是为您转义并添加标签和东西。 If you just want the raw input, you can print it directly.如果您只想要原始输入,则可以直接打印。

This won't fix all of your issues, though, because you're echoing it inside a JavaScript string, so you need to escape certain characters.但是,这并不能解决您的所有问题,因为您在 JavaScript 字符串中回显它,因此您需要转义某些字符。 json_encode() should do the trick, but see the question "Pass a PHP string to a Javascript variable (including escaping newlines)" for more details. json_encode()应该可以解决问题,但有关更多详细信息,请参阅问题“将 PHP 字符串传递给 Javascript 变量(包括 escaping 换行符)”

Try this:尝试这个:

echo(mb_convert_encoding(the_title(), "UTF-8", "HTML-ENTITIES"));

see if this works for ya看看这是否适合你

$convmap = array (0x0, 0xffff, 0, 0xffff);
//$str = mb_decode_numericentity (the_title(), $convmap, 'UTF-8' );
$str = mb_decode_numericentity ("&#038;", $convmap, 'UTF-8' );
echo $str;

http://www.php.net/manual/en/function.mb-decode-numericentity.php http://www.php.net/manual/en/function.mb-decode-numericentity.php

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

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