Posted By

alvaroisorna on 07/04/06


Tagged

database php tagging


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

luman
mate
meth
alexsancho
kawikak
nicolaspar
demods
pixelhandler
vali29
nb109


sanitizeTags, version 1.0 (Daniel Mota aka IceBeat)


 / Published in: PHP
 

Taken from Daniel Mota at

http://icebeat.bitacoras.com/archivo/199/

  1. /* sanitizeTags, version 1.0: http://icebeat.bitacoras.com
  2.  * (c) 2005 Daniel Mota aka IceBeat <[email protected]>
  3.  *
  4. /*--------------------------------------------------------------------------*/
  5. function sanitizeTags($tags='',$mode=0) {
  6. $tags = strtolower($tags);
  7. $new_tags = array();
  8. $words = array();
  9. $_tags = preg_split('!(\+|-|\||\s)!',$tags,-1,PREG_SPLIT_DELIM_CAPTURE);
  10. if(count($_tags)>1) {
  11. $tag = current($_tags);
  12. if(strlen($tag)>=4) $new_tags[] = $tag;
  13. next($_tags);
  14. while ($del = current($_tags)) {
  15. next($_tags);
  16. $tag = current($_tags);
  17. if(strlen($tag)>=4) {
  18. switch ( $del ) {
  19. case '+': $new_tags[] = '+'.$tag; break;
  20. case '-': $new_tags[] = '-'.$tag; break;
  21. default: $new_tags[] = $tag; break;
  22. }
  23. $words[] = $tag;
  24. }
  25. next($_tags);
  26. }
  27. } elseif(strlen($_tags[0])>=4) {
  28. $new_tags[] = $_tags[0];
  29. $words[] = $_tags[0];
  30. }
  31. $tags = join(chr(32),$new_tags);
  32. $query = preg_match('!(\+|-)!', $tags) ? "('+".$tags."' IN BOOLEAN MODE)": "('".$tags."')";
  33. $query = 'WHERE MATCH (tags) AGAINST '.$query;
  34. if($mode==1) return $words;
  35. if($mode==2) return array($words,$query);
  36. return $query;
  37. }
  38.  
  39. //ejemplo
  40. //$q = 'SELECT * from mitabla '.sanitizeTags('icebeat+web');

Report this snippet  

You need to login to post a comment.