# Adding spaces in search phrase between alphabetics and numbers

Often times combination of alphabetic and word inputs may yield no result if the input phrase is typed in without spaces.

### Issue example

A post content contains these words:

```
Hello world 1235!
```

The user types in the following search phrase:

```
hello12345
```

The search will yield therefore **NO result** - because the search phrase "hello12345" **does not match** the content string in any way (phrase or it's parts are not a substring of the content).

For us humans this would be an obvious match, but the code can only look for parts of the search input. Words are by default only separated by word separators - commas, spaces, tabs etc..

### Solution

[What is this, and where do I put this custom code?](/safe-coding-guideline.md)

<pre class="language-php"><code class="lang-php"><strong>// Adding whitespace between numeric and alphabetical words
</strong><strong>add_filter(
</strong>	'asp_query_args',
	function ( $args ) {
		$previous_char = '';
		$new_string    = '';
		foreach ( mb_str_split($args['s']) as $char ) {
			if (
			( is_numeric($previous_char) &#x26;&#x26; ctype_alpha($char) ) ||
			( is_numeric($char) &#x26;&#x26; ctype_alpha($previous_char) )
			) {
				$new_string .= ' ' . $char;
			} else {
				$new_string .= $char;
			}
			$previous_char = $char;
		}
		$args['s'] = $new_string;

		return $args;
	}, 10, 1
);
</code></pre>

The custom code above will look for alphanumerics and numbers (and vice versa) in sequence and add spaces between them. Therefore typing in:

```
hello12345
```

..becomes:

```
hello 12345
```

This will match the original post content, therefore resolves the issue.

### Combined solution with extra character removal

In this knowledge base is a solution to replace some special word boundary characters, in case needed, here is a combination of the two codes.

[What is this, and where do I put this custom code?](/safe-coding-guideline.md)

```php
// Special character removal & adding whitespace between numeric and alphabetical
add_filter(
	'asp_query_args',
	function ( $args ) {
		$characters   = "',._-?!"; // Type characters one after another
		$replace_with = ' ';     // Replace them with this (space by default)

		$args['s'] = str_replace(str_split($characters), $replace_with, $args['s']);

		$previous_char = '';
		$new_string    = '';
		foreach ( mb_str_split($args['s']) as $char ) {
			if (
				( is_numeric($previous_char) && ctype_alpha($char) ) ||
				( is_numeric($char) && ctype_alpha($previous_char) )
			) {
				$new_string .= ' ' . $char;
			} else {
				$new_string .= $char;
			}
			$previous_char = $char;
		}
		$args['s'] = $new_string;

		return $args;
	}, 10, 1
);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://knowledgebase.ajaxsearchpro.com/miscellaneous/other/adding-spaces-in-search-phrase-between-alphabetics-and-numbers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
