-3

I am trying to replace a single quote (') with another quote (), but I can't seem to get anything to work. Also, how can I make this work on multiple strings ($text, $title, $notice)?

input: don't

output: don’t

I am trying this:

$text = str_replace(array("'",'"'), array('’'), $text);    
$text = htmlentities(str_replace(array('"', "'"), '’', $text);
$text = htmlentities(str_replace(array('"', "'"), '’', $_POST['text']));
$text = str_replace("'" ,"’",$text);
$text = str_replace("'" ,"’",$text);
$text = str_replace(array("'"), "’", $text);
$text = str_replace(array("\'", "'", """), "’", htmlspecialchars($text));
$text = str_replace(array('\'', '"'), '’', $text);
$text = str_replace(chr(39), chr(146), $text);
$text  = str_replace("'", "&ampquot;", $text); 

None of this works.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Aleza
  • 15
  • 4
  • 1
    1) Why exactly do you want to do this? 2) And what is the exact output of `highlight_string($text)`? – Rizier123 May 08 '16 at 14:15
  • If @Quasimodosclone's answer doesn't work could you provide a reproducible example? I tried your first method with `don't` and it works fine. Obviously it won't work with double quotes, but for single quotes it does work. – Chris May 08 '16 at 14:25
  • 1
    Possible duplicate of [Replacing quotes with str\_replace() is not working, and how do I remove spaces in a PHP string?](http://stackoverflow.com/questions/37094014/replacing-quotes-with-str-replace-is-not-working-and-how-do-i-remove-spaces-i) – splash58 May 08 '16 at 14:28
  • "Also, how can I make this work on multiple strings ($text, $title, $notice)?" - Put it into a user-defined function. – mario May 08 '16 at 14:34
  • @splash58 - "possible" duplicate? Obviously that's an exact repetition of the previously ask question by the same author. :) – Pinke Helga May 08 '16 at 14:43
  • I don't know the procedures on SO, but since the previous question didn't receive any attention, it could be reasonable to delete the old question and give the new one a chance. – Pinke Helga May 08 '16 at 14:47

2 Answers2

5

When you use an array as replacements, give as many replacements as you give needles. Else use a simple string.

<?php
$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), '’', $text);  
echo $text;

$text = 'Peter\'s cat\'s name is "Tom".';
$text = str_replace(array("'",'"'), array('’', '`'), $text);  
echo $text;
?>

To perform that task on multiple variables you could do

<?php
$text   = 'Peter\'s cat\'s name is "Tom".';
$title  = 'Peter\'s cat\'s name is "Tom".';
$notice = 'Peter\'s cat\'s name is "Tom".';

foreach([&$text, &$title, &$notice] as &$item)
  $item = str_replace(array("'",'"'), '’', $item);  

echo "text: $text<br>title: $title<br>notice: $notice";
?>
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
  • Just noting that there is nothing wrong with having more needles than replacements. more replacements than needles would be wrong though. – chiliNUT May 08 '16 at 16:21
  • @chiliNUT - There is nothing "wrong" meaning no error is thrown, that's correct. However, it does not work as expected by the question owner. The behavior equals to filling the replacements array with empty strings. – Pinke Helga May 08 '16 at 17:47
1

I tried using preg_replace() and it worked perfectly first time:

$text = "Someone told me about a 'bacon and cheese sandwich'";
$text = preg_replace("#\'#", '’', $text);
echo $text;

Output
Someone told me about a ’bacon and cheese sandwich’

Give that a go.

Hypernami
  • 138
  • 1
  • 8