PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Post from a newB. I have a peice of code which as I understand provides the search operators. Currently it only allows for searching one word. I would like for the search to insert % wherever there is a space and at the begining and end of the search.
e.g. 'please search this' would search for '%please%search%this%' .
This may not be enough code to work from or im heading down the wrong route. If so then appologies. Any help appreciated.
You should use the appropriate *_real_escape_string() or whatever instead of addslashes unless that is what CRM_Utils_Type::escape() is doing, then you can delete the addslashes.
$val = strtolower( addslashes( $val ) );
// works but would replace sequential spaces with multiple %
$val = str_replace( ' ', '%', $val );
// replaces sequential spaces with only one %
$val = preg_replace('/[ ]+/', '%', $val);
$val = "%$val%";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
At the moment the search for 'please search this' would search for '%please%search%this%' . Meaning the result has to have all three. Is there a change that can be made so it searches for. %please% or %search% or %this% ?
chris7889 wrote:Thank you very much for your help.
At the moment the search for 'please search this' would search for '%please%search%this%' . Meaning the result has to have all three. Is there a change that can be made so it searches for. %please% or %search% or %this% ?
$condition = "WHERE `field_name` LIKE '%" . implode("%' OR `field_name` LIKE '%", explode(" ", $val)) . "%'";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.