Recent Post

Dear reader, this blog has been moved to here

Thursday, 12 June 2014

Interprete and Parse Meta Robot Values Using PHP



Hey reader, you really need to see this PHP meta robots parser?

So damn easy, now lets get our hands dirty ;). Am writing a function that interprets the meta robots which receives arguments of strings (e.g noindex,nofollow), you can grab this with the get_meta_tags() function then let me start the function to parse this.



In this function I return 4 values which are:

  • 113 = nofollow and noindex

  • 116 = index but nofollow

  • 117 = noindex but follow

  • 200 = index and follow


Now starting the function, firstly lets check if the argument has a value, if empty then return 200
if(!$content) {
return 200;
}

Else lets do our stuff by exploding argument value into simple arrays but wait! Do you know some people put spaces after the comma in their meta robots and some don't? (e.g NOINDEX,NOFOLLOW or NOINDEX, NOFOLLOW). Now let's handle this to avoid logical error, we will parse the array and trim() each array value then gather them back to array :)
else {
$content = explode(',',$content);
foreach($content as $value) {
$d[] = trim($value);
}

Now lets do the comparison by checking if value is in array:
if(in_array('noindex', $d)==true and in_array('nofollow',$d)==true) {
return 113;
}
elseif(in_array('noindex', $d)==true and in_array('nofollow',$d)==false) {
return 117;
}
elseif(in_array('noindex',$d)==false and in_array('nofollow', $d)==false) {
return 200;
}
elseif(in_array('noindex', $d)==false and in_array('nofollow', $d)==true) {
return 116;
}
else {
return 200;
}


Now the full code:
<?php
function interprete_meta_robots($content) {
/*
Name: Meta Robots Interpreter
Author: Don Jajo

You can modify these codes to your desire

=====RETURNING VALUES======
113 = nofollow and noindex
116 = index but nofollow
117 = noindex but follow
200 = index and follow
==========================
*/
if(!$content) {
return 200;
}
else {
$content = explode(',',$content);
foreach($content as $value) {
$d[] = trim($value);
}

if(in_array('noindex', $d)==true and in_array('nofollow',$d)==true) {
return 113;
}
elseif(in_array('noindex', $d)==true and in_array('nofollow',$d)==false) {
return 117;
}
elseif(in_array('noindex',$d)==false and in_array('nofollow', $d)==false) {
return 200;
}
elseif(in_array('noindex', $d)==false and in_array('nofollow', $d)==true) {
return 116;
}
else {
return 200;
}
}
}
?>

Email Newsletter



Smiley :)
:D
:)
:[
;)
:D
:O
(6)
(A)
:'(
:|
:o)
8)
(K)
(M)

No comments:

Post a Comment