4

I have following textarea field in question form where user can post their questions:

<tr>
    <th><label><?php echo $this->lang->line('question'); ?>: </label></th>
    <td><textarea  value="" id="ques_desc" name="ques_desc"
        class="validate[required] text-input" > </textarea> </td>
</tr>

The problem is when user copies and paste question from world or excel, then symbol ' becomes and get stored in the database in the same format.

Example:

Check men's and women's rest rooms

becomes

Check men’s and women’s rest rooms

And when user searches for Check men's in the search field then Check men’s does not appear in the search result.

I tried str_replace function with and ' , it does not worked. example:

$ques_desc = str_replace("’","'",$ques_desc);

does not replace to '.

What might be the way to replace each with ' ?

EDIT:

When I do this on view page:

<?php 
$ques_desc1 = "Check men’s and women’s rest rooms "; 
$ques_desc = str_replace("’","'",$ques_desc1); 
echo $ques_desc; 
?>

It displays:

Check men's and women's rest rooms

It worked!

When I do this on controller:

$ques_desc1 = $this->input->post('ques_desc');
$ques_desc = str_replace(" ",".",$ques_desc1);

It displays: Take O’pening Stock Count to Take.O’pening.Stock.Count

it also worked!

But when I do this:

$ques_desc1 = $this->input->post('ques_desc');
$ques_desc = str_replace("’","'",$ques_desc1);

It does not work. Nothing changes.

Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
  • 1
    Wouldn't it be a better solution to fix your search engine? For example, if your database ever needed to store the word `café` and people wanted to search with `cafe`, then maybe you could change the `é` to `e`, but then you'd start mangling non-English text. – icktoofay Apr 24 '13 at 04:19
  • 1
    I expect it to be unicode char which just looks like a quote. You need to know which char it is actually. Would be nice if you could post a hexdump of your data. Have coded a [hexdump](https://github.com/metashock/Hexdump) package for PHP once. Maybe you can try it. – hek2mgl Apr 24 '13 at 04:42

4 Answers4

3

Use str_replace like this

echo str_replace("’","'","Check men’s and women’s rest rooms");

Output

Check men's and women's rest rooms

Code Fiddle

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • 2
    So long as there is no Unicode where `0x27` is part of a multi-byte character. – alex Apr 24 '13 at 04:20
  • As I already said, I tried str_replace before posting to database it does not work at all. $ques_desc = str_replace("'","’",$ques_desc); does not replaces ’ to '. – Vijaya Pandey Apr 24 '13 at 04:21
  • 2
    @alex: So long as it's UTF-8, that can't happen. No bytes with high bits clear will be part of a multibyte sequence. – icktoofay Apr 24 '13 at 04:21
  • @icktoofay I just figured that out when trying to make an example. :) – alex Apr 24 '13 at 04:24
  • Yeah. It works on viewpage. When I do this: `` It displays: `Check men's and women's rest rooms` But, when data goes to controller: `$ques_desc1 = $this->input->post('ques_desc'); $ques_desc = str_replace("’","'",$ques_desc1);` Here, `str_replace("’","'",$ques_desc1)` does not work. But `str_replace(" ",".",$ques_desc1)` works perfectly. – Vijaya Pandey Apr 24 '13 at 05:29
  • 1
    @Venonj - please include working/not working example in your question (edit it) above. It's easier to read then. – bestprogrammerintheworld Apr 24 '13 at 05:30
  • I got the solution. Check it to my answer please. – Vijaya Pandey Apr 24 '13 at 06:43
3

I found the solution of my own problem.

I tried this:

$ques_desc1 = $this->input->post('ques_desc');
$ques_desc = iconv('UTF-8', 'ASCII//TRANSLIT', $ques_desc1);

Here, iconv('UTF-8', 'ASCII//TRANSLIT', $ques_desc1) replaces Microsoft Word version of single and double quotations marks “ ” ‘ ’ with regular quotes ' and ".

and it worked perfectly.

Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
0

You have mistake in str_replace syntax. you should put as first parameter and ' as second so change this:

$ques_desc = str_replace("'","’",$ques_desc);

to :

$ques_desc = str_replace("’","'",$ques_desc);
Amir
  • 4,089
  • 4
  • 16
  • 28
0

Sounds like you're having trouble with Microsoft Word's smart quotes. Check out this answer for a couple of tips for handling that.

I did a quick and dirty test and this particular method worked:

$quotes = array(
    "\xC2\xAB"     => '"', // « (U+00AB) in UTF-8
    "\xC2\xBB"     => '"', // » (U+00BB) in UTF-8
    "\xE2\x80\x98" => "'", // ‘ (U+2018) in UTF-8
    "\xE2\x80\x99" => "'", // ’ (U+2019) in UTF-8
    "\xE2\x80\x9A" => "'", // ‚ (U+201A) in UTF-8
    "\xE2\x80\x9B" => "'", // ‛ (U+201B) in UTF-8
    "\xE2\x80\x9C" => '"', // “ (U+201C) in UTF-8
    "\xE2\x80\x9D" => '"', // ” (U+201D) in UTF-8
    "\xE2\x80\x9E" => '"', // „ (U+201E) in UTF-8
    "\xE2\x80\x9F" => '"', // ‟ (U+201F) in UTF-8
    "\xE2\x80\xB9" => "'", // ‹ (U+2039) in UTF-8
    "\xE2\x80\xBA" => "'", // › (U+203A) in UTF-8
);
$str = strtr($str, $quotes);
Community
  • 1
  • 1
Jonathan
  • 316
  • 1
  • 10