2011-07-13 40 views
4

三十一有the_title()返回一些文本,在这种情况下Blue & Whinyphp,如何将特殊字符转换为文本?

我们可以看到的问题是,&角色看起来与众不同

如何打开Blue & WhinyBlue & Whiny我tryed:htmlspecialchars_decode(the_title())html_entity_decode(the_title())htmlspecialchars(the_title())没事了。

我想&转换为&

没有太多的代码共享,我只是这样做:<?php the_title() ?>,我也得到Blue &#038; Whiny。如果我使用get_the_title()它不会显示任何东西

任何想法? 谢谢

edit1。生病分享一些代码:

<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> 

我试过了一切。这开始困扰我...

+0

你尝试过'html_entity_decode(the_title())'它自己吗?如果是的话,结果是 –

+0

我做了什么,我试过所有单独的,它不会解码 – Patrioticcow

+0

你想要显示'蓝色& Whiny' as'Blue&Whiny'或做别的事情? – Balanivash

回答

9

html_entity_decode()是做到这一点的正确方法。

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

会产生:

蓝&嘀咕

如果它不工作,请确保您没有其他问题 - 如传递一个字符串,它是双编码或稍后再次在字符串上运行htmlentities()

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

仔细检查与文字字符串和var_dump()输出,你应该看到的解码版本。然后var_dump(the_title()),以确保你实际上是通过你认为你是html_entity_decode()

+0

nope。请参阅我编辑过的文章 – Patrioticcow

5

html_entity_decode应该做的伎俩。如果不是,请尝试指定第三个参数$charset

喜欢的东西:

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

nope。请参阅我编辑过的文章 – Patrioticcow

+0

这个解决方案最终可以为我工作如果它对未来的读者有所帮助,下面是我的具体“Disqus”实现:'disqus_title ='<?= addslashes(html_entity_decode(get_the_title($ post-> ID),ENT_QUOTES,'UTF-8'))?> |公司名''('json_encode()')也可以代替'addslashes',但语法需要稍微调整) – mhulse

2

试试这个:

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

结果相同,字符不转换 – Patrioticcow

+0

你确定你在分号&";“? – 2011-07-13 19:03:55

3

the_title()目录直接打印标题,因此直接添加html_entity_decode()无法工作。但是,您可以停止使用其第三个函数参数进行打印。例如。

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

还有get_the_title(),不直接打印标题,但它需要你想要的标题后,在对比与the_title的ID,打印当前职位中The Loop称号。所以,你需要做这样的事情:

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

而实际上,你应该能够简单地做:

<?php echo $post->post_title ?> 

的唯一原因,这些效用函数是有逃避你的东西,并添加标签和东西。如果您只需要原始输入,则可以直接打印。

但是,这并不能解决所有问题,因为您在JavaScript字符串中回显它,所以您需要转义某些字符。 json_encode()应该诀窍,但请参阅"Pass a PHP string to a Javascript variable (including escaping newlines)"以获取更多详细信息。

相关问题