Interprete and Parse Meta Robot Values Using PHP
- June 12, 2014
- PHP
- No comments
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
1 2 3 |
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 🙂
1 2 3 4 5 |
else { $content = explode(',',$content); foreach($content as $value) { $d[] = trim($value); } |
Now lets do the comparison by checking if value is in array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?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; } } } ?> |
Related
James John
Software Engineer