문제

For example say a user is selling a computer. The title of the product and the description of the product will go in the meta tags like this.

<title><?php echo $product_title; ?></title>
<meta name="description" content="<?php echo $product_desc; ?>" />

Is that correct way of doing things on a user based website? If so, what if the product description is like 500 characters? Is that too long for meta tags? Should it be cut?

도움이 되었습니까?

해결책

Yes that approach is fine but you should also use htmlspecialchars. For example:

<title><?php echo htmlspecialchars($product_title); ?></title>
<meta name="description" content="<?php echo htmlspecialchars($product_desc); ?>" />

Regarding the length of the meta description. Google has the answer:

Meta descriptions can be any length, but search engines generally truncate snippets longer than 160 characters. It is best to keep meta descriptions between 150 and 160 characters.

So, if you would like to do the work for the search engines you can truncate the meta description by using substr.

<?php
    echo '<title>'.htmlspecialchars($product_title).'</title>'.
         '<meta name="description" content="'.htmlspecialchars(substr($product_desc,0,160)).'" />';
?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top