문제

I am absolutely sure this will have been covered but have searched and cannot see how to apply strtolower in this form. Maybe I'm looking at the wrong solution. I have a form:

<form id="demo-b" method="get" action="<?php echo home_url( '/' ); ?>">
<div class="input-prepend">
<input class="skip-search" type="search" placeholder="Search" id="prependedInput" name="s" value="<?php the_search_query(); ?>">
<input type="hidden" name="sentence" value="1">
</div>
</form>

I am trying to convert the search input to lower case and from what I've seen PHP strtolower may be the solution but I'm not sure how to correctly insert it. Can anyone assist? Perhaps I've got the wrong solution?

Additional Info...

I am trying to sanitize search input to convert user input to the term used on my site. There are just a couple of terms and the following code is used (this is Wordpress and apologies if I've posted in the wrong place but I thought it was a specific PHP issue)...

/*sanitise search queries manually*/
$search_replacements = array(
'tilt shift' => 'tilt-shift',
'depth of focus' => 'depth of field'
);
function modify_search_term($request_vars) {
global $search_replacements;
if (!empty($request_vars['s']) && !empty($search_replacements[$request_vars['s']])) {
    $request_vars['s'] = $search_replacements[$request_vars['s']];
}
return $request_vars;
}
add_filter('request', 'modify_search_term');

The problem is that search terms are not case sensitive but as soon as I add them to this array they become case sensitive. I could add multiple versions of the term but it seemed sensible to convert the search term to lower case so that it returns the correct results. Perhaps this is the code that needs editing to correctly edit the form input on the server side? The client can use capitals as much as they like as far as I am concerned.

도움이 되었습니까?

해결책

You will need to use Javascript for this because PHP is processed after you submit the form or if you use AJAX then that's a different and inefficient story.

Javascript

<input type="text" value="" onKeyPress="this.value = this.value.toLowerCase();" onKeyUp="this.value = this.value.toLowerCase();" onKeyDown="this.value = this.value.toLowerCase();">

JSFiddle

Server-side PHP

$_POST['s'] = strtolower($_POST['s']);

// Do whatever you want with the search term now because it is lower-case
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top