I have this custom query that Im working on and I need to be able to query output values as well as actual stored values.
Example:
$string = "This is #*&^% a test";
$string = preg_replace('/[^(\x20-\x7F)]*/','', $string);
$string_clean = $string
So if I echo $string_clean, I'll get this:
This is a test
Which is fine, but the issue is that the $string_clean value isn't a stored value (just an output), so I dont know how to use its value in a query, being that the query results are based on stored database values.
Im using a multi array capability of the wordpress query (here). Basically, I use two keys in the array with AND as the meta_compare.
Ideally, I would like to be able to query both value (stored and output). Im interested in this as a search functionality for my query.
Can someone help me with this? How do I do it?. Or if someone knows of a better solution please let me know.
UPDATE:
This is the code that Im working with. I couldn't find any reliable solutions to implement REGEXP. Basically, what this should do, in theory, is use the php preg_replace
function to remove/replace any special characters in the string and query the new clean string, that will then be used in the query's argument. Here it is:
*The initial value of the variable are submitted through a simple html form (not included).
if(isset($_POST['providername'])) {
$providername = $_POST['providername'];
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $providername))
{
$clean = $providername;
// Replace other special chars
$specialCharacters = array(
',' => '',
'&' => 'and',
'@' => 'at',
'.' => '',
'+' => '',
'=' => '',
'/' => '',
'*' => '',
'!' => '',
'(' => '',
')' => '',
'$' => '',
'#' => '',
'%' => '',
'\'' => '',
'_' => '',
'^' => '',
'-' => ''
);
while (list($character, $replacement) = each($specialCharacters)) {
$clean = str_replace($character, '-' . $replacement . '-', $clean);
}
// Remove all remaining other unknown characters
$clean = preg_replace('/[^a-zA-Z0-9\-]/', ' ', $clean);
$clean = preg_replace('/^[\-]+/', '', $clean);
$clean = preg_replace('/[\-]+$/', '', $clean);
$clean = preg_replace('/[\-]{2,}/', '', $clean);
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $providername)) {
$providername = $clean;
} else {
$providername = $_POST['providername'];
}
}
}
if(isset($_POST['providerloc'])) {
$provider_location = $_POST['providerloc'];
}
This is the multi array argument:
$args = array(
'count_total' => true,
'role' => 'subscriber',
'offset' => 8,
'number' => 8,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'jabber',
'value' => $provider_location,
'compare' => 'LIKE'
) ,
array(
'key' => 'keywordcontent',
'value' => $providername,
'compare' => 'LIKE'
)
)
);
After testing this solution, I get no results when trying to search for the "clean" outputted value.
For example:
If...
$value = "G-P's Workin' Barn";
After applying preg_replace
, the output will be...
GPs Workin Barn
Now...if I search for "GPs Workin Barn" (the output value), I get no results. But when I search for "G-P's Workin' Barn" (the original value), it shows the result for that term. So it seems that the query isn't able to process the output value. As I thought, this makes sense because the query is searching against stored values in the database...There should be a way for the query to interpret/translate the output value and compare it against the stored value. After searching online, the closest solution that relates to what I need is the post_where filter for posts [credit @Rarst]. There doesn't seem to be a similar filter for users. Even so, I need a way to search using REGEXP against actual variable values.
Any thoughts or solutions? Thanks
posts_where
that @Rarst suggested, actually, rather than hacking core. Take the "clean" value you want, explode it on '', and implode with an arbitrary number of non-alphanumeric characters (ie '[^a-zA-Z0-9\-]*') in between each character. Then use the "REGEXP" operator in your posts_where clause.