plnet /lib/rdfapi-php/api/sparql/FilterFunctions.php

Language PHP Lines 136
MD5 Hash 1893f2e0bde69d3ecccf748044d49936 Estimated Cost $1,755 (why?)
Repository https://github.com/komagata/plnet.git View Raw File View Project SPDX
  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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?PHP
/**
* List of functions used to evaluate the FILTER statements in
* SPARQL queries.
*
* @package sparql
* @author   Tobias Gauss <tobias.gauss@web.de>
* @version	 $Id$
*
*/

 


/**
* Evaluates the regex() function. Returns true if the regex is matched false if not.
*
* @param String  $string   the string which has to be evaluated
* @param String  $pattern  the regex pattern
* @param String  $flags    additional flags like "i"
* @return boolean
*/
function regex($string,$pattern,$flags = ''){
	$string = trim($string);
	$pattern = trim($pattern);
	if(strpos($string,"str_")===0){
		$string = substr($string,4);
		$pattern = substr($pattern,4);
		$flags = substr($flags,4);
	}else{
		return false;
	}
	if(preg_match('/'.$pattern.'/'.$flags,$string))
	return true;
	else
	return false;
}

/**
* Evaluates the dateTime() function.Tries to convert a date string into
* a unix timestamp.
*
* @param  String $string the date string
* @return integer        the corresponding unix timestamp
*/
function dateTime($string){
	$string = trim($string);
	if(strpos($string,"str_")===0)
	$string = substr($string,4);

	$time = strtotime($string);
	if($time == -1)
	return $string;
	else
	return $time;
}


/**
* Evaluates the langMatches() function. Return true if the lang tag matches false if not.
*
* @param String $lang_range the string.
* @param String $lang_tag the regex pattern
* @return boolean
*/
function langMatches($lang_range,$lang_tag){

	if($lang_range == null)
	return false;

	if(strpos($lang_range,"str_")===0)
	$lang_range = substr($lang_range,4);
	if(strpos($lang_tag,"str_")===0)
	$lang_tag = substr($lang_tag,4);

	if(strtolower($lang_range) == strtolower($lang_tag))
	return true;
	$tags =  preg_match_all("/[^\-\s].[^\-\s]*/",$lang_range,$hits);
	if($tags){
		if($lang_tag == '*')
		return true;
		foreach($hits[0] as $tag){
			if(strtolower($tag) == strtolower($lang_tag))
			return true;
		}
		return false;
	}else{
		return false;
	}
}

/**
* Evaluates the str() function. Returns the string representation of a 
* variable or RDF term.
*
* @param  String $string the string
* @return boolean
*/
function str($string){
	$str = preg_match("/\".[^\"]*\"|\'.[^\']*\'/",$string,$hits);
	if($str != 0){
		return "str_".$hits[0];
	}else{
		if(strpos($string,"str_")===0){
			return $string;
		}else{
			if(strpos($string,"uri_")===0)
				return "str_".substr($string,4);
			if(strpos($string,'<')==0)
				return "str_".substr($string,1,-1);
		}
	}
	return false;
}

/**
* Evaluates the lang() function. Returns lang tag of a Literal.
*
* @param  String $string the string.
* @return String the lang tag or false if there is no language tag.
*/
function lang($string){
	$str = preg_match("/\".[^\"]*\"@(.[^\s]*)|\'.[^\']*\'@(.[^\s]*)/",$string,$hits);
	if($str){
		if($hits[1] != null)
		return $hits[1];
		else
		return $hits[2];
	}else{
		return false;
	}
}



?>
Back to Top