Unwanted hyphens

Hi

I would like to enter all records in the database without hyphens and have the script strip the query of the hyphens before it queries the database. At least, that is how I think it could work.

I found some code online and wonder if it would do the trick, or if it might create problems:

Select REPLACE(PART_FIELD_NAME, ‘-’, ‘’)

At the moment I have a line in the script as follows:

$sql = “SELECT Image, Chain, Country, City, Top as ‘’, Medium as ‘’, Low as ‘’ FROM Chains WHERE Country LIKE ? OR City LIKE ?”;

What would I have to change? My knowledge of php is “zero”!

Thank you

Try this:

$item =  str_replace('-', '', $item);

Google: Php Manual str_replace()

1 Like

Hi John

Thank you. How would I change the line above, or is it supposed to go on its own, and where?

$sql = “SELECT Image, Chain, Country, City, Top as ‘’, Medium as ‘’, Low as ‘’ FROM Chains WHERE Country LIKE ? OR City LIKE ?”; // Your query string

$prepare = $mysqli->prepare($sql); // Prepare your query string
$prepare->bind_param(‘ss’, $searchq, $searchq); // Bind the placeholders to your search variables
// s = string | i = integer | d = double | b = blob
$prepare->execute(); // Execute the prepared statement

$prepare->store_result(); // Store the results for later checking

Try this:

$searchq = str_replace( '-', '', $searchq );

$prepare->bind_param('ss', $searchq, $searchq);

Hi John

I’m afraid, either I did something wrong, or it doesn’t work

There is a record “Figueira da Foz”. Just to test, I entered “Figueira-da-Foz” and it returned “Nothing found”

$sql = “SELECT Image, Chain, Country, City, Top as ‘’, Medium as ‘’, Low as ‘’ FROM Chains WHERE Country LIKE ? OR City LIKE ?”; // Your query string

$prepare = $mysqli->prepare($sql); // Prepare your query string

$searchq = str_replace( ‘-’, ‘’, $searchq );

$prepare->bind_param(‘ss’, $searchq, $searchq); // Bind the placeholders to your search variables
// s = string | i = integer | d = double | b = blob
$prepare->execute(); // Execute the prepared statement

EDIT

Wait! I’m not fully awake yet… I mus put $City somewhere… Where? Or does it apply to all fields?

Try this:

echo '<br />Before: '. $searchq;
$searchq = str_replace( '-', '', $searchq );
echo '<br />After: '. $searchq;
die;

Can you think of a solution?

1 Like

Assuming that code was to substitute the one you gave me before in the same place, it does not work and the page does not go through returning instead on a white background

Before: %figueira-da-foz%
After: %figueiradafoz%

It does not work but it is strange because before your code and with your code if I search for Paris, not only I get Paris, but also “Villeparisis” beacuse of the double %%. So, why don’t I get Figueira da Foz with figueiradafoz? After all, I get it with just Figueira or even figue

Your script was not not working.

The script I supplied was to echo on the screen the search variable before and after applying the str_replace(); function results and then to stop the remaining script.

Can you see the problem?

Once you have rectified the problem remove the echo and die statements;

No, I don’t.

Did you read my edit in the last post?

Yes I read your edit.

Did you follow the link I supplied that gave a detailed explanation of the parameters passed to str_replace() and the results?

No, I don’t.

What else have you tried?

Ok, thanks John

If you managed to find a solution and for the benefit of others who read this topic, please supply your working script.

I just gave up, that’s all. I will live with what I have

You should not give up, only take a rest and approach the problem with new vigour :slight_smile:

Your Post #7
It does not work but it is strange because before your code and with your code if I search for Paris, not only I get Paris, but also “Villeparisis” beacuse of the double %%. So, why don’t I get Figueira da Foz with figueiradafoz? After all, I get it with just Figueira or even figue

The script I supplied would have shown the following results:

Before: Figueira-da-Foz 
After:  FigueiradaFoz 

// The second str_replace() parameter should be changed to a single space and not an empty space:

$searchq = str_replace( '-', ' ', $searchq);

// New results:
Before: Figueira-da-Foz 
After:  Figueira da Foz 

If there is still a problem try removing trailing spaces which are not shown but can effect the search query:

$searchq = trim( $searchq );

This will strip the hyphens from both the country and city fields you are searching for

$sql = "SELECT Image, Chain, Country, City, Top as '', Medium as '', Low as '' FROM Chains WHERE Country LIKE ? OR City LIKE ?"; 
$prepare = $mysqli->prepare($sql);
$prepare->bind_param('ss', str_replace( '-', '', $country), str_replace( '-', '', $city));
$prepare->execute();
1 Like

Hi

Thank you both

I did not give up. It is just that I was getting more confusd than when I started.

In fact, the code John gave me was not entirely correct

Inside the brackets it should read ‘-’.’ ', i.e. substitute hyphen with space, and not with nothing.

It is working now.

I am going to have a look at felgall’s solution next.

Thank you both.

Hi fellgal

Is there an advantage in your code, against the one I have in place now?

Hi @qim
The original script I supplied assumed either a leading or trailing hyphen, unfortunately your $searchq hyphen was between words. The debugging script highlighted the missing spaces.

No doubt you have learnt something new about Php rather than just copying and pasting supplied solutions :slight_smile:

Hi felgall

Can this sort of code be extended to strip words of accents?

Starting with grave, acute, and circumflex?

something like

$converted = iconv('UTF-8', 'ASCII//TRANSLIT', $text);

?