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('\r\n', '\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('\r\n', '\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 ;)

Related Posts Related Websites
Help sharing and Flatter me ;)

Share

15 Comments

  1. [...] I wrote some functions for better SEO in PHP, but I realized that the Keywords are not so [...]

  2. ramon says:

    my site uses utf-8 and language is Catalan (Romance language contains words with accents such as: és, demà …., and the apostrophe is also used as: d’una, l’utilitat….) the problem is that the keywords show wrong the accented characters.
    How could I fix it?

  3. Chema says:

    Hola! soc de barcelona, entenc el problema ;)

    But I will answer to you in English if you don’t mind since like this everybody can understand it.

    Instead of using this functions I do recommend to you this last php class: http://neo22s.com/phpseo/

    For accents I should be no problem, but for the apostrophe, I do recommend to you to replace it for an empty space. Then it would disappear and consider it as a single word!

    Saluts!

  4. ramon says:

    hola chema, també sóc català

  5. ramon says:

    chema,
    pel que fa als apòstrofs he provat d’afegir això:
    $text = str_replace (“‘”, ‘ ‘, $text);//replace for apostrofs

    però els continua mostrant igualment:
    d\ avui, d\ agricultura,

  6. Chema says:

    com pases els parametres? magic quotes? crec que si…aquest es el problema ;)

  7. Chema says:

    no em refereixo com crides la funció? com pases el parametres?

  8. ramon says:

    de moment estic utilitzant les pàgines que has deixat a phpclasses.org, la d’exemple i la de la classe

  9. Chema says:

    pots probar aixo:

    if ($_POST) $text=stripslashes($_POST["text"]);

    ;) crec que funcionará!

    saluts!

  10. ramon says:

    chema, amb la modificació ja funciona, els apostrofs han desaparegut, però en els keywords encara em surten: d agricultura o d avui.
    Si hi ha un espai, no haurien de surtir, oi?.
    Gràcies per la teva paciència :)

  11. Chema says:

    no no hi haurin de sortir, primer has de esborrar (cambi per espai en blanc) l’apostrof y despres netejar els blancs.

    Tranquil! ara m’em vaig, si vols continuar aixo sius plau usa el forum ;) es mes facil per mi!

    records!

  12. flashguy says:

    I have been searching for sites related to this. Glad I found you. Thanks

  13. Chema says:

    enjoy, and welcome!

Leave a Reply

Follow me