og:description'content'未终止的字符串文字 [英] og:description 'content' unterminated string literal
问题描述
可能重复:
未终止的字符串文字
Possible Duplicate:
unterminated string literal
我在设置og:description时遇到问题,其功能如下...
I have a problem setting og:description with followed function...
function createFacebookMeta($title, $fcUrl, $fcImg, $fcDesc){
$fcDesc = (strlen($fcDesc) > 100) ? substr($fcDesc,0,150).'...' : $fcDesc;
$faceBook = "<script type=\"text/javascript\">
$(document).attr('title', '".$title."');
$('meta[property=\"og:title\"]').attr('content', '".$title."');
$('meta[property=\"og:url\"]').attr('content', '".$fcUrl."');
$('meta[property=\"og:image\"]').attr('content', '".$fcImg."');
$('meta[property=\"og:description\"]').attr('content', '".$fcDesc."');
FB.XFBML.parse();
</script>";
echo $faceBook;
}
作为回应,我进入了萤火虫
as response i get in firebug
未终止的字符串文字
$('meta[property="og:description"]').attr('content', 'Logos gedruckt<br /> //breaks here
即使我使用剥离标签,它也会报告相同的内容...如果我未设置og:description,则会采用默认元描述(此处为np),长度大致相同,因为我读到fb从中获取了大约300个字符最大
even if i use striptags it reports same ... if i don´t set og:description default meta description is taken (np here) which is about the same lenght, as i read that fb takes arround 300 chars from it max
谢谢
$ fcDesc是数据库结果
$fcDesc is db result
$fcDesc = "Logos gedruckt
<br>
100% Baumwolle
<br>
Vorne: Logo
<br>
Rücken: Cash Ruls";
(产品描述)
推荐答案
您将以破坏代码的方式将字符串输出到javascript代码中.
You are outputting strings into javascript code in a way that it breaks the code.
发生这种情况是因为您没有正确编码javascript的PHP值.
That happens because you do not properly encode the PHP values for javascript.
一种简单的方法是使用 json_encode()
函数:
An easy way to do that is to use the json_encode()
function:
$faceBook = '<script type="text/javascript">
$(document).attr(\'title\', ' . json_encode($title) . '); ....';
在需要为javascript编码PHP变量的值时使用它. JSON是javascript的子集,因此效果很好.
Use it whenever you need to encode the value of a PHP variable for javascript. JSON is a subset of javascript so this works really well.
另外,您可能希望简化描述字符串:
Additionally you might want to simplify the description string:
$simplified = preg_replace('/\s+/', ' ', strip_tags($fcDesc));
这确实删除了其中的HTML <br>
标记,然后对空格进行了规范化.
This does remove the HTML <br>
tags you have in there and then normalizing whitespaces.
还让我们看看json_encode
的作用:
echo json_encode($simplified), "\n";
输出:
"Logos gedruckt 100% Baumwolle Vorne: Logo R\u00fccken: Cash Ruls"
如您所见,json_encode
不仅要注意添加引号,而且还要将字符串中的字符正确编码为unicode序列.
As you can see, json_encode
takes not only care of adding the quotes but to also properly encode characters in the string into unicode sequences.
在原始字符串中,您有换行符.在javascript中,字符串中不能包含换行符(可以在PHP中,但不能在javascript中).在原始字符串上使用json_encode
也可以解决该问题:
In your original string you had line-breaks. In javascript you can not have line-breaks in string (you can have in PHP, but not in javascript). Using json_encode
on your original string does fix that, too:
"Logos gedruckt\n<br>\n100% Baumwolle\n<br>\nVorne: Logo\n<br>\nR\u00fccken: Cash Ruls"
如您所见,换行符在输出中正确地写为\n
.只要记住json_encode
,将它用于所有放入javascript标记的变量即可.这将使您的代码稳定.
As you can see, the line-breaks are correctly written as \n
in the output. Just remember json_encode
, use it for all your variables you put into the javascript tag. That will make your code stable.
这篇关于og:description'content'未终止的字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!