Hi Dormllich
and where do I put that in the code above?
I saw this, but again I don’t know what to fo with it, (not in php!)
// normalize data (remove accent marks) using PHP’s intl extension
$data = normalizer_normalize($data);
Hi Dormllich
and where do I put that in the code above?
I saw this, but again I don’t know what to fo with it, (not in php!)
// normalize data (remove accent marks) using PHP’s intl extension
$data = normalizer_normalize($data);
Internationalisation is not the most intuitive topic. You would either have to read through the provided documents in the Manual or ask someone versed in it.
Hi
But your answer: where do I place it and how?
I’m totally useless with php…
you mean you don’t know where you want the characters replaced?
Try this:
// START cleaning input string
// DEBUG
echo '<br />Debug: str_replace(...)';
echo '<br />Before str_replace( '. $searchq .')';
$searchq = str_replace( '-', ' ', $searchq );
echo '<br />After str_replace( '. $searchq .')';
echo '<br /><br />';
echo '<br />Debug: iconv(...)';
echo '<br />Before iconv( '. $searchq .')';
$searchq = iconv('UTF-8', 'ASCII//TRANSLIT', $searchq);
echo '<br />After: iconv('. $searchq .')';
echo '<br /><br />';
echo '<br />Notice the output and refresh your browser to try another input';
echo '<br />Repeat until satisfied THEN remove all echo and die statements';
die;
// continue with cleaned input
$prepare->bind_param('ss', $searchq, $searchq); // Bind the placeholders to your search variables
Thank you John
It does not work beacuse what it is doing is getting rid of the letter with the accent, rather than replacing it with the same letter without accent, i.e. café > cafe. What I would get with this is “caf”
EDIT
Maybe I did not notice it before but the current code with just
$searchq = str_replace( ‘-’, ’ ', $searchq );
seems to be deleting accented letters too!
You can try here http://pintotours.net/Search/Search1.php
and enter “Setúbal” , the town where José Mourinho was born…
it is in the database as “Setubal” only, Look at the result
ANOTHER EDIT
Would this be any good?
You don’t need fancy regexps to filter the swedish chars, just use the strtr function to “translate” them, like:
$your_URL = “www.mäåö.com”;
$good_URL = strtr($your_URL, “äåöë etc…”, “aaoe etc…”);
echo $good_URL;
If your site is international then it will be a major problem to cater for all languages.
A far better method would be to replace your input search text box with a drop-down list box populated with countries and cities from your database.
Happy Googling and looking forward to seeing your results
There will be about 10000…and in some countries (e.g. USA) hundreds.
I am only concerned with grave, acute , tilde and circumflex, at the moment:
if à, á, â, ã convert to a
and the same for the other vowels
Take another look at str_replace(…) because it accepts arrays. I have already supplied the link in a previous post.
$old = array( 'à', 'á', 'â', 'ã' );
$new = arrray( 'a', 'a', 'a', 'a' );
echo '<br />Before: ' .$searchq;
$searchq = str_repace( $old, $new, $searchq );
echo '<br />After: ' .$searchq;
echo '<br />Notice the output and refresh your browser to try another input';
echo '<br />Repeat until satisfied THEN remove all echo and die statements';
die;
//
I pasted that to replace the earlier line
and got an error
Fatal error: Call to undefined function arrray() in /home/pintotou/public_html/Search/Search1.php on line 49
I entered “Guimarães” and you can try it as your code is still there
Like so:
$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
$old = array( ‘à’, ‘á’, ‘â’, ‘ã’ );
$new = arrray( ‘a’, ‘a’, ‘a’, ‘a’ );
echo '
Before: ’ .$searchq;
$searchq = str_repace( $old, $new, $searchq );
echo '
After: ’ .$searchq;
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->store_result(); // Store the results for later checking
array - not arrray
Thank you felgall
Hi John
Same result: it just gets rid of the letter altogether!
Before: %Guimares%
After: %Guimares%
Notice the output and refresh your browser to try another input
Repeat until satisfied THEN remove all echo and die statements
The query was, and I’ve rechecked: “Guimarães” with a tilde on the a
The problem is with the current code that is doing its job regarding hyphens but deleting letters with accent as well…
CORRECTION
I’m blaming the current code but in this instance it was not even there! So, the problem comes from before
$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
I wish I could understand this:
[sourcecode language=’php’] function just_clean($string)
{
// Replace other special chars
$specialCharacters = array(
‘#’ => ”,
‘$’ => ”,
‘%’ => ”,
‘&’ => ”,
‘@’ => ”,
‘.’ => ”,
‘€’ => ”,
‘+’ => ”,
‘=’ => ”,
‘§’ => ”,
‘\’ => ”,
‘/’ => ”,
);
while (list($character, $replacement) = each($specialCharacters)) {
$string = str_replace($character, ‘-‘ . $replacement . ‘-‘, $string);
}
$string = strtr($string,
“ÀÁÂÃÄÅ? áâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ”,
“AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn”
);
// Remove all remaining other unknown characters
$string = preg_replace(‘/[^a-zA-Z0-9-]/’, ‘ ‘, $string);
$string = preg_replace(‘/[1]+/’, ”, $string);
$string = preg_replace(‘/[-]+$/’, ”, $string);
$string = preg_replace(‘/[-]{2,}/’, ‘ ‘, $string);
return $string;
}
- ↩︎
Try this:
// put this function at the top of your file
// http://stackoverflow.com/questions/1333062/how-to-remove-unwanted-characters-from-a-string
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;
}
// use the function to clean your $searchq input string
This function has been tried and tested and will certainly go in my library
Hi
I’m afraid it didn’t work, exactly as you posted.
Am I supposed to change anything, like making $string into $city ?
I also found something in stackoverflow and again does not work if used as I found it.
http://stackoverflow.com/questions/3371697/replacing-accented-characters-php
$val = iconv(‘ISO-8859-1’,‘ASCII//TRANSLIT’,$val);
$val = “á|â|à|å|ä ð|é|ê|è|ë í|î|ì|ï ó|ô|ò|ø|õ|ö ú|û|ù|ü æ ç ß abc ABC 123”;
echo iconv(‘UTF-8’,‘ASCII//TRANSLIT’,$val);
$city = iconv(‘ISO-8859-1’,‘ASCII//TRANSLIT’,$city);
$city = “á|â|à|å|ä ð|é|ê|è|ë í|î|ì|ï ó|ô|ò|ø|õ|ö ú|û|ù|ü æ ç ß abc ABC 123”;
echo iconv(‘UTF-8’,‘ASCII//TRANSLIT’,$city);
Maybe I should change $val to $city?
Try this:
Perfect!
How do I get that in my script?
Thanks
When you say place the code at the head of the page, do you mean that literally, like so?
<?php
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;
}
$results = true;
I thought you meant immediately after connection.
Anyway I tried at the very top and still does not work like in your test
The source code is supplied at the bottom of the page.
As mentioned in post #33
// use the function to clean your $searchq input string
We crossed messages. Please, see above
I tried in various places.
This is the current php code with your addition yet in another place
<?php
$results = true;
// 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: /TEMP/SOS/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
$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
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;**
}
$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’;
}
?>
As it is, it still does not work. It deletes the accented letter
http://www.tizag.com/phpT/phpfunctions.php
Please take the time to study this tutorial on Php functions, when you confident you can recognize and use functions in your own scripts, practice with some examples.
Afterwards take a look at the source code that can be copied and pasted from my online demo. Make sure the copy is working both locally and on your server. Try making some changes and note the results.
When you are fully familiar with using functions, copy and paste the function from my script into your script, test, debug and upload your final version.