2011-07-13 43 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)"以獲取更多詳細信息。

相關問題