Categories
PHP

SEO Functions for PHP

It’s known for all of us that SEO it’s really important at any web.

Because of that at many projects I used few functions to make the SEO work properly.

Here you have 2 important one’s:

Description: We erase all the possibles HTML characters, returns and we keep just 220 chars.

function getMetaDescription($text){//returns a text with a proper meta description
	$text = strip_tags($text);//erase possible html tags
	$text = str_replace (array('rn', 'n', '+'), ' ', $text);//replace possible returns
	$text = substr($text, 0, 220);//we need only 220 characters
	return $text."...";
}

Meta Keyword:Erases special chars such as HTML, returns, commas…deletes repeated values and keywords smaller than 3.

function getKeyWords($text){//from the given text
        $text = strip_tags($text);//erase possible html tags
	$text = str_replace (array('rn', 'n', '+'), ' ', $text);//replace possible returns
	$text = str_replace (array('-','(',')','+','-'), '', $text);//replace not valid character
	$text = str_replace (' ', ',', $text);//replace for comas
	//erase minor than 3, and repeated ones
	$arrText=explode(",",$text);
	$arrText=array_unique($arrText);//erase the repeated values
	$text=array();
	foreach ($arrText as &$value) {
 	   if (strlen($value)>=3) array_push($text,$value);
	}
	unset($value);
	$text=implode(",",$text);
	return $text;
}

In the keywords function we can improve how many keywords to return (normally max of 25), also when we take from that 25 ones, we should count how many repetitions and erase the less repeated ones…I will work on this and paste it here when I have it 😉