When my last word in an excerpt text has accents (strange characters)

I run a search engine and in that system Im showing search results but also some text that starts with the search word.

But I have one problem and I cannot solve it.

The problem is this: when I search for a word, and the last word of my text gives out an accent, that the last word seems strange, or I have this "&". or ".

Could you please help me understand what is wrong here?

My problem code:

$search = $url[1];
$read = $pdo->prepare("SELECT * FROM pages WHERE title LIKE ? OR content LIKE ? LIMIT ?,?"); 
$read->bindValue(1, "%$search%", PDO::PARAM_STR);
$read->bindValue(2, "%$search%", PDO::PARAM_STR);
$read->bindParam(3, $begin,PDO::PARAM_INT);
$read->bindParam(4, $max,PDO::PARAM_INT);
$read->execute();
$searchPos = stripos($result['content'],$search);
$searchLen = strlen($search);
$result_text = '"'.substr($result['content'], $searchPos, $searchLen + 35).'..."';
echo '<p>'.strip_tags($result_text).'</p>';

      

(And im using tinymce editor to insert into database)

Insert content into database using tinymce Im doing this:

$f['content'] =$_POST['content'];

      

Then I paste like:

$insert->bindParam(4,$f['content']);

      

+3


source to share


3 answers


The problem is that you are working with multibyte UTF-8 strings using non-multibyte functions.

Here's your code modified to work correctly with UTF-8:

$search = $url[1];
$read = $pdo->prepare("SELECT * FROM pages WHERE title LIKE ? OR content LIKE ? LIMIT ?,?"); 
$read->bindValue(1, "%$search%", PDO::PARAM_STR);
$read->bindValue(2, "%$search%", PDO::PARAM_STR);
$read->bindParam(3, $begin,PDO::PARAM_INT);
$read->bindParam(4, $max,PDO::PARAM_INT);
$read->execute();
$searchPos = mb_stripos($result['content'],$search,0,'utf-8');
$searchLen = mb_strlen($search,'utf-8');
$result_text = '"'.mb_substr($result['content'], $searchPos, $searchLen + 35, 'utf-8').'..."';
echo '<p>'.strip_tags($result_text).'</p>';

      



Note mb_*

Using functions:

+5


source


  • Make sure your files are saved in utf-8 encoding (utf-8 without BOM), any advanced editor will be able to change the file encoding, notepad ++ has this option in its main menu.
  • Make sure your table encoding is utf8 like utf8_general_ci
  • Add charset = utf8 to your dabatase connection string or if you are using php 5.3.6 or later use "SET NAMES utf8" before your queries.
  • Set character encoding in header php, html, etc.
  • Use php multibyte string functions for string lookups, comparisons, etc.


Opponents 1, 3, and 5 solve this problem for the most part.

-1


source


On the last line in your script, use the htmlentities function.

echo '<p>'.htmlentities($result_text,ENT_QUOTES,"UTF-8").'</p>';

      

This should solve the "strange nature" problem: P

-1


source







All Articles