Hello everyone the search is not working. For example the tags contain Php, Ajax, HTML 5 when you search Php or Ajax there is a result but if you search two words or more such as HTML 5 there is no result.
my model Codes:
public function getTagsMatch($limit=null, $tags, $offset=null) {
$match = $tags;
$this->db->from('threads');
$this->db->where('status', 1);
$search_query_values = explode(' ', $match);
$counter = 0;
foreach ($search_query_values as $key => $value) {
if ($counter == 0) {
$this->db->like('tags', $value);
}
$counter++;
}
$this->db->order_by('pin_post', 'DESC');
$this->db->order_by('id', 'DESC');
$this->db->limit($limit);
$this->db->offset($offset);
$query = $this->db->get();
return $query->result_array();
}
Well...
$search_query_values = explode(' ', $match);
This line takes in query values and "explodes" the string using spaces. So if you input "HTML 5", it will actually search for tags like "HTML" or "5". Consider using a different character for exploding.
For example:
$search_query_values = explode(',', $match);
And the function call would be something like this:
getTagsMatch(NULL, 'Ajax,HTML 5,PHP', NULL);
A few more notes:
Default parameters should be at the end of an argument list (function foo(bar0, bar1=0, bar2='') { ... }, not randomly placed).
Consider using less variables. There is absolutely no reason to have $tags, $match and $search_query_values - one is enough. You might consider using 3 variables a semantic advantage, but it actually makes your code more difficult to read.
The problem of this was on my controller because. I customize the tags but the fact is Im having a trouble how to explain this since Im not good in explaining such thing. I will just post some of the codes with some explanation.
My tags search will base on what is the tags slug for example website.com/tags/html-5
so my controller codes:
public function tags($tags) {
$tag = search_title($tags);
$data['result'] = $this->topic_model->getTagsMatch($tag);
}
and my search_title function code:
function search_title($str, $separator = ' ') {
$str = ucwords(strtolower($str));
foreach (array('-', '\'') as $delimiter) {
if (strpos($str, $delimiter)!==false)
{
$str =implode($delimiter, array_map('ucfirst', explode($delimiter, $str)));
}
}
$str = str_replace('-',' ',$str);
$str = str_replace('%20',' ',$str);
$str = str_replace('%26','&',$str);
$str = str_replace('%27',' ',$str);
$str = str_replace('%28',' ',$str);
$str = str_replace('%29',' ',$str);
return trim(stripslashes($str));
}
the problem of it is on this line
$data['result'] = $this->topic_model->getTagsMatch($tag);
so I change the $tag to str_replace("-"," ",$tags)
Related
I created an input text, where I write something that I want to search in an array.
I already use stristr() and stripos() but whit this I get only the same words, example:
<?php
$value[0]= 'animals';
//other array values
$arrayResp = [];
foreach($valuefounded as $value)
{
if(stristr($value, "animals") !== FALSE)
{
$arrayResp[]= $value;
}
}
how can I get the same result, if I type grammatical mistake like "animls"
There's no simple function to achieve this, but you could build something like with soundex, levenshtein or similar functions. To do so, you have to split the input text into words and do the calculation for each word.
But you should keep in mind, this works only for complete words.
function search_in_string($search, $text, $max = 2) {
$words = explode(' ', $text);
foreach ($words as $word) {
if (levenshtein($word, $search) <= $max) {
return strpos($text, $word);
}
}
return false;
}
I recently updated my site to PHP 5.6, and since doing that, I started getting errors with the e modifier.
Here is the function causing the issue:
function blockPrepareDisplay() {
static $search = array('/(.)#(.)/se');
static $replace = array('"&#" .sprintf("%03d", ord("\\1")) .";#&#" .sprintf("%03d", ord("\\2")) . ";";');
$resarray = array();
foreach (func_get_args() as $var) {
$var = htmlspecialchars($var, ENT_QUOTES);// Prepare var
$var = preg_replace($search, $replace, $var);
$var = preg_replace('/&#/', '&#', $var);
$var = str_replace(" "," ",$var);
$var = str_replace("&","&",$var);
$resarray[] = $var;// Add to array
}
if (func_num_args() == 1) {// Return vars
return $resarray[0];
} else {
return $resarray;
}
}
I understand that the line static $search = array('/(.)#(.)/se'); is the line with the e modifier and I know that its deprecated. A friend explained that to me, and explained that I wound need to replace the preg_replace with a preg_replace_callback. I have looked over at php.net and all over here, but I think it made me more confuesd than ever.
I have tried for a couple of weeks now many different things and I got one to stop spitting the error, but I am positive that its wrong, and I prefer it to be right than wrong. I did this in replacement of the preg_replace
$var = preg_replace_callback(
$search,
function($replace){
foreach($replace as $replaces){
return $replaces;
}
},
$var
);
Instead of the $var = preg_replace($search, $replace, $var);.
I also did remove the e modifier. Can anyone point out my mistake and give an example of how I should have this to be right?
The argument of the callback function is the array of matching groups.
I don't understand why you are using an array for the search and the replacement parts, I'd do:
$var = "abc#def.org";
$var = preg_replace_callback(
'/(.)#(.)/',
function($match) {
return sprintf("&#%03d;#&#%03d;", ord($match[1]), ord($match[2]));
},
$var
);
echo $var,"\n";
Output:
abc#def.org
In few words, I am trying to replace all the "?" with the value inside a variable and it doesn't work. Please help.
$string = "? are red ? are blue";
$count = 1;
update_query($string, array($v = 'violets', $r = 'roses'));
function update_query($string, $values){
foreach ( $values as $val ){
str_replace('?', $val, $string, $count);
}
echo $string;
}
The output I am getting is: ? are red ? are blue
Frustrated by people not paying attention, I am compelled to answer the question properly.
str_replace will replace ALL instances of the search string. So after violets, there will be nothing left for roses to replace.
Sadly str_replace does not come with a limit parameter, but preg_replace does. But you can actually do better still with preg_replace_callback, like so:
function update_query($string, $values){
$result = preg_replace_callback('/\?/', function($_) use (&$values) {
return array_shift($values);
}, $string);
echo $string;
}
You forgot to set it equal to your variable.
$string = str_replace('?', $val, $string, $count);
You probably want to capture the return from str_replace in a new string and echo it for each replacement, and pass $count by reference.
foreach ( $values as $val ){
$newString = str_replace('?', $val, $string, &$count);
echo $newString;
}
This is the best and cleanest way to do it
<?php
$string = "? are red ? are blue";
$string = str_replace('?','%s', $string);
$data = array('violets','roses');
$string = vsprintf($string, $data);
echo $string;
Your code edited
$string = "? are red ? are blue";
update_query($string, array('violets','roses'));
function update_query($string, $values){
$string = str_replace('?','%s', $string);
$string = vsprintf($string, $values);
echo $string;
}
Ok guys here is the solution from a combination of some good posts.
$string = "? are red ? are blue";
update_query($string, array($v = 'violets', $r = 'roses'));
function update_query($string, $values){
foreach ( $values as $val ){
$string = preg_replace('/\?/', $val, $string, 1);
}
echo $string;
}
As mentioned, preg_replace will allow limiting the amount of matches to update. Thank you all.
You can solve this in two ways:
1) Substitute the question marks with their respective values. There are a hundred ways one could tackle it, but for something like this I prefer just doing it the old-fashioned way: Find the question marks and replace them with the new values one by one. If the values in $arr contain question marks themselves then they will be ignored.
function update_query($str, array $arr) {
$offset = 0;
foreach ($arr as $newVal) {
$mark = strpos($str, '?', $offset);
if ($mark !== false) {
$str = substr($str, 0, $mark).$newVal.substr($str, $mark+1);
$offset = $mark + (strlen($newVal) - 1);
}
}
return $str;
}
$string = "? are red ? are blue";
$vars = array('violets', 'roses');
echo update_query($string, $vars);
2) Or you can make it easy on yourself and use unique identifiers. This makes your code easier to understand, and more predictable and robust.
function update_query($str, array $arr) {
return strtr($str, $arr);
}
echo update_query(':flower1 are red :flower2 are blue', array(
':flower1' => 'violets',
':flower2' => 'roses',
));
You could even just use strtr(), but wrapping it in a function that you can more easily remember (and which makes sense in your code) will also work.
Oh, and if you are planning on using this for creating an SQL query then you should reconsider. Use your database driver's prepared statements instead.
I've the following method which allows me to protect MySQL entities:
public function Tick($string)
{
$string = explode('.', str_replace('`', '', $string));
foreach ($string as $key => $value)
{
if ($value != '*')
{
$string[$key] = '`' . trim($value) . '`';
}
}
return implode('.', $string);
}
This works fairly well for the use that I make of it.
It protects database, table, field names and even the * operator, however now I also want it to protect function calls, ie:
AVG(database.employees.salary)
Should become:
AVG(`database`.`employees`.`salary`) and not `AVG(database`.`employees`.`salary)`
How should I go about this? Should I use regular expressions?
Also, how can I support more advanced stuff, from:
MAX(AVG(database.table.field1), MAX(database.table.field2))
To:
MAX(AVG(`database`.`table`.`field1`), MAX(`database`.`table`.`field2`))
Please keep in mind that I want to keep this method as simple/fast as possible, since it pretty much iterates over all the entity names in my database.
If this is quoting parts of an SQL statement, and they have only complexity that you descibe, a RegEx is a great approach. On the other hand, if you need to do this to full SQL statements, or simply more complicated components of statements (such as "MAX(AVG(val),MAX(val2))"), you will need to tokenize or parse the string and have a more sophisticated understanding of it to do this quoting accurately.
Given the regular expression approach, you may find it easier to break the function name out as one step, and then use your current code to quote the database/table/column names. This can be done in one RE, but it will be tricker to get right.
Either way, I'd highly recommend writing a few unit test cases. In fact, this is an ideal situation for this approach: it's easy to write the tests, you have some existing cases that work (which you don't want to break), and you have just one more case to add.
Your test can start as simply as:
assert '`ticked`' == Tick('ticked');
assert '`table`.`ticked`' == Tick('table.ticked');
assert 'db`.`table`.`ticked`' == Tick('db.table.ticked');
And then add:
assert 'FN(`ticked`)' == Tick('FN(ticked)');
etc.
Using the test case ndp gave I created a regex to do the hard work for you. The following regex will replace all word boundaries around words that are not followed by an opening parenthesis.
\b(\w+)\b(?!\()
The Tick() functionality would then be implemented in PHP as follows:
function Tick($string)
{
return preg_replace( '/\b(\w+)\b(?!\()/', '`\1`', $string );
}
It's generally a bad idea to pass the whole SQL to the function. That way, you'll always find a case when it doesn't work, unless you fully parse the SQL syntax.
Put the ticks to the names on some previous abstraction level, which makes up the SQL.
Before you explode your string on periods, check if the last character is a parenthesis. If so, this call is a function.
<?php
$string = str_replace('`', '', $string)
$function = "";
if (substr($string,-1) == ")") {
// Strip off function call first
$opening = strpos($string, "(");
$function = substr($string, 0, $opening+1);
$string = substr($string, $opening+1, -1);
}
// Do your existing parsing to $string
if ($function == "") {
// Put function back on string
$string = $function . $string . ")";
}
?>
If you need to cover more advanced situations, like using nested functions, or multiple functions in sequence in one "$string" variable, this would become a much more advanced function, and you'd best ask yourself why these elements aren't being properly ticked in the first place, and not need any further parsing.
EDIT: Updating for nested functions, as per original post edit
To have the above function deal with multiple nested functions, you likely need something that will 'unwrap' your nested functions. I haven't tested this, but the following function might get you on the right track.
<?php
function unwrap($str) {
$pos = strpos($str, "(");
if ($pos === false) return $str; // There's no function call here
$last_close = 0;
$cur_offset = 0; // Start at the beginning
while ($cur_offset <= strlen($str)) {
$first_close = strpos($str, ")", $offset); // Find first deep function
$pos = strrpos($str, "(", $first_close-1); // Find associated opening
if ($pos > $last_close) {
// This function is entirely after the previous function
$ticked = Tick(substr($str, $pos+1, $first_close-$pos)); // Tick the string inside
$str = substr($str, 0, $pos)."{".$ticked."}".substr($str,$first_close); // Replace parenthesis by curly braces temporarily
$first_close += strlen($ticked)-($first_close-$pos); // Shift parenthesis location due to new ticks being added
} else {
// This function wraps other functions; don't tick it
$str = substr($str, 0, $pos)."{".substr($str,$pos+1, $first_close-$pos)."}".substr($str,$first_close);
}
$last_close = $first_close;
$offset = $first_close+1;
}
// Replace the curly braces with parenthesis again
$str = str_replace(array("{","}"), array("(",")"), $str);
}
If you are adding the function calls in your code, as opposed to passing them in through a string-only interface, you can replace the string parsing with type checking:
function Tick($value) {
if (is_object($value)) {
$result = $value->value;
} else {
$result = '`'.str_replace(array('`', '.'), array('', '`.`'), $value).'`';
}
return $result;
}
class SqlFunction {
var $value;
function SqlFunction($function, $params) {
$sane = implode(', ', array_map('Tick', $params));
$this->value = "$function($sane)";
}
}
function Maximum($column) {
return new SqlFunction('MAX', array($column));
}
function Avg($column) {
return new SqlFunction('AVG', array($column));
}
function Greatest() {
$params = func_get_args();
return new SqlFunction('GREATEST', $params);
}
$cases = array(
"'simple'" => Tick('simple'),
"'table.field'" => Tick('table.field'),
"'table.*'" => Tick('table.*'),
"'evil`hack'" => Tick('evil`hack'),
"Avg('database.table.field')" => Tick(Avg('database.table.field')),
"Greatest(Avg('table.field1'), Maximum('table.field2'))" => Tick(Greatest(Avg('table.field1'), Maximum('table.field2'))),
);
echo "<table>";
foreach ($cases as $case => $result) {
echo "<tr><td>$case</td><td>$result</td></tr>";
}
echo "</table>";
This avoids any possible SQL injection while remaining legible to future readers of your code.
You could use preg_replace_callback() in conjunction with your Tick() method to skip at least one level of parens:
public function tick($str)
{
return preg_replace_callback('/[^()]*/', array($this, '_tick_replace_callback'), $str);
}
protected function _tick_replace_callback($str) {
$string = explode('.', str_replace('`', '', $string));
foreach ($string as $key => $value)
{
if ($value != '*')
{
$string[$key] = '`' . trim($value) . '`';
}
}
return implode('.', $string);
}
Are you generating the SQL Query or is it being passed to you? If you generating the query I wouldn't pass the whole query string just the parms/values you want to wrap in the backticks or what ever else you need.
EXAMPLE:
function addTick($var) {
return '`' . $var . '`';
}
$condition = addTick($condition);
$SQL = 'SELECT' . $what . '
FROM ' . $table . '
WHERE ' . $condition . ' = ' . $constraint;
This is just a mock but you get the idea that you can pass or loop through your code and build the query string rather than parsing the query string and adding your backticks.
I am using an api to retrieve data from another server the data returned is something like this:
accountid=10110 type=prem servertime=1263752255 validuntil=1266163393
username= curfiles=11 curspace=188374868 bodkb=5000000 premkbleft=24875313
This is a whole string I need two values out of whole string, I am currently using preg_match to get it, but just to learn more and improve my coding is there any other way or function in which all values are automatically convert to array?
Thank You.
Sooo, my faster-than-preg_split, strpos-based function looks like this:
function unpack_server_data($serverData)
{
$output = array();
$keyStart = 0;
$keepParsing = true;
do
{
$keyEnd = strpos($serverData, '=', $keyStart);
$valueStart = $keyEnd + 1;
$valueEnd = strpos($serverData, ' ', $valueStart);
if($valueEnd === false)
{
$valueEnd = strlen($serverData);
$keepParsing = false;
}
$key = substr($serverData, $keyStart, $keyEnd - $keyStart);
$value = substr($serverData, $valueStart, $valueEnd - $valueStart);
$output[$key] = $value;
$keyStart = $valueEnd + 1;
}
while($keepParsing);
return $output;
}
It looks for an equals character, then looks for a space character, and uses these two to decide where a key name begins, and when a value name begins.
Using explode is the fastest for this, no matter what.
However, to answer you question, you can do this many ways. But just because you can, doesn't mean you should. But if you really wanna make it weird, try this.
UPdated using strpos
$arr = array();
$begin = 0;
$str = trim($str); # remove leading and trailing whitespace
while ($end = strpos($str, ' ', $begin)) {
$split = strpos($str, '=', $begin);
if ($split > $end) break;
$arr[substr($str, $begin, $split-$begin)] = substr($str, $split+1, $end-$split-1);
$begin = $end+1;
}
try out parse_str maybe you need to do str_replace(' ', '&', $string); before