Unwanted hyphens

Thank you very much. John

I will look at it and test again

1 Like

Hi John

I’m afraid at my age things don’t come too easily… You’ll get there one day!

I have spent all afternoon trying to understand why your Test works, but no matter what I do I can’t replicate it here. Something to do with my keyboard?

The Source code is of little use as it does not show me

  1. where exactly you placed the code **within the script **

This is where it is at the moment but it does nothing new. After this code comes the connection with password, etc

<?php

$results = true;

function just_clean($string)
{
$string = str_replace(‘-’, ’ ', $string);
$string = preg_replace
(
‘~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i’,
‘$1’,
htmlentities($string, ENT_COMPAT, ‘UTF-8’)
);

return $string;
}

// Find out if the user accessed the file directly
//if($_SERVER[‘REQUEST_METHOD’] == ‘GET’) {

// header(‘Location:/TEMP/SOS/Search1.php’); // redirect the user if the form was not submitted.
// die(); // Ignore anything after the die.

//} elseif($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

this is a much neater method of detecting post, nothing posted, send them on their way

if(!isset($_POST)){
header(‘Location: http://pintotours.net/Search/Search1.php’); // redirect the user if the form was not submitted.
die(); // Ignore anything after the die.

}

Somehow, I feel that it should come after the connection when it is in touch with the database, but I have tried it in many positions and can’t get it to work.

Anyway, I appreciate the time you took trying to help me.

Cheers.

Hi @qim,

The function should work if it is in the file that calls the MySQL functions.

Use the echo statements to show the $searchq value before and after calling the just_clean() function.

$searchq has to be correct before it is used in the MySQL functions.

[off topic] I don’t know how old you are but I very much doubt I will ever reach your current age :slight_smile:

This is what I have including the debug

<?php

$results = true;

function just_clean($string)
{
$string = str_replace(‘-’, ’ ', $string);
$string = preg_replace
(
‘~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i’,
‘$1’,
htmlentities($string, ENT_COMPAT, ‘UTF-8’)
);

return $string;
}

// Find out if the user accessed the file directly
//if($_SERVER[‘REQUEST_METHOD’] == ‘GET’) {

// header(‘Location:Search1.php’); // redirect the user if the form was not submitted.
// die(); // Ignore anything after the die.

//} elseif($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

this is a much neater method of detecting post, nothing posted, send them on their way

if(!isset($_POST)){
header(‘Location:http://pintotours.net/Search/Search1.php’); // redirect the user if the form was not submitted.
die(); // Ignore anything after the die.

}

// Your database preferences
// We are using constants instead of variables for this
// You can use either or
define(‘HOST’, ‘localhost’); // Database host
define(‘pintotou_‘, ‘root’); // Database username
define(’
’, ‘root’); // Database password
define(‘pintotou_search’, ‘’); // Database

$mysqli = new mysqli(‘localhost’, ‘pintotou_', '’, ‘pintotou_search’); // Connect to the database using MySQLi_* OOP and constants
if($mysqli->connect_errno) {
// Do not die or display any MySQL errors in this area.
die(‘Unable to connect to the mysql server’);
}

$searchq = filter_var(“%{$_POST[‘keyword’]}%”, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); // Sanitize the string

$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

// DEBUG
echo ‘
Debug: str_replace(…)’;
echo ‘
Before str_replace( ‘. $searchq .’)’;
$searchq = str_replace( ‘-’, ’ ', $searchq );
echo ‘
After str_replace( ‘. $searchq .’)’;
echo ‘

’;

echo ‘
Debug: iconv(…)’;
echo ‘
Before iconv( ‘. $searchq .’)’;
$searchq = iconv(‘UTF-8’, ‘ASCII//TRANSLIT’, $searchq);
echo ‘
After: iconv(’. $searchq .‘)’;
echo ‘

’;

echo ‘
Notice the output and refresh your browser to try another input’;
echo ‘
Repeat until satisfied THEN remove all echo and die statements’;
die;

$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

// Use num_rows to check if the results return a 0 or 1. 0 meaning false and 1 meaning true
if($prepare->num_rows) {

$prepare->bind_result($image, $chain, $country, $city, $top, $medium, $low); // Append variables to the columns you specified

}else
{
$results = false;
$searchMsg = ‘There were no search results for ‘’. $searchq .‘’!’;
}
if(strlen(trim($_POST[‘keyword’])) <= 3)
{
$results = false;
$searchMsg = ‘Please enter at least 3 characters’;
}
?>

The result of the debug

Debug: str_replace(…)
Before str_replace( %guimares%)
After str_replace( %guimares%)

Debug: iconv(…)
Before iconv( %guimares%)
After: iconv(%guimares%)

Notice the output and refresh your browser to try another input
Repeat until satisfied THEN remove all echo and die statements

Just don’t drink too much!

Try this:

$searchq = $_POST['keyword'];

Ok, I placed that code on the line below

and this time it returns the whole word exactly as it is entered, i.e. with the accent still there, which means that it returns a “Not found”

Ok that has eliminated the % characters, now pass $searchq as a parameter to just_clean();


$searchq = just_clean( $searchq );

Did you check out the Php function tutorials?

Where do I put this?

I did, but you don’t learn php in 10 minutes…

A simplified flow might be

  • define variable, in this case assigning a “raw” input value from a form to it
  • process variable, includes validation, sanitizing, and otherwise getting it as desired
  • use the variable

Call the just_clean() function immediately before the above line.

Thanks

So I put that line of code before this one

[quote=“John_Betong, post:45, topic:209249”]
$searchq = filter_var(“%{$POST[‘keyword’]}%”, FILTERSANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); // Sanitize the string[/quote]

The word GuimarĂŁes was returned without the accented letter, like before

I had deleted the earlier code

$searchq = $_POST[‘keyword’];

It is quite late here so signing out and hope you solve the problem.

Good night :slight_smile:

Thanks John

But this has been a long thread and I think I’m going to give up. The whole code is in earlier posts and if the experts can’t work it out, it will not be me without any php knowledge that’s going to crack it.

Let me thank you again for your efforts.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.