0

I'm trying to remove the ' out of my string.

Here's my code:

$page_title = strtolower(wp_title( '', false, 'right' ));
echo $page_title;
echo "<br/>";
$clean = preg_replace('/[^A-Za-z0-9\-]/', '', $page_title);
echo $clean;

Output:

regio’s 
regio8217s

Why does it return 8217 instead of ''?

Thanks in advance

Stijn Hoste
  • 836
  • 1
  • 7
  • 17
  • 2
    I'm not getting the same result you do: http://codepad.org/Ir5ZIsLk – Alexandre Danault Jun 19 '13 at 19:01
  • 3
    The `’` is inserted using the character reference `’`. – Gumbo Jun 19 '13 at 19:02
  • Hi Alex-Info.net, that's because you used the static string. Like @Gumbo said, the ' is inserted using the character reference. Any idea how we can remove that certain character? I'm learning by the minute... :) – Stijn Hoste Jun 19 '13 at 19:09

2 Answers2

1

Your quote has been converted to its unicode value (&#8217;) (see here for example). It's a special character, not a standard one.

Hobo
  • 7,536
  • 5
  • 40
  • 50
  • The OP asked why, which is the question I answered. Though fair point. A solution would depend on where the OP wanted the problem addressed - when the value's stored, or when it's retrieved – Hobo Jun 19 '13 at 19:12
  • I want the ' to be removed out of the string. – Stijn Hoste Jun 19 '13 at 19:14
  • You could use [str_replace](http://php.net/manual/en/function.str-replace.php) explicitly, replacing '’' with '' before your `preg_replace`. The problem with that is it's not very generic - you could get other escaped characters in there. Maybe `preg_replace(/&[^;]+;/, '', $page_title);` would be better. But it's still open to problems – Hobo Jun 19 '13 at 19:17
  • Mhm that's right. Let's try "carré" for example. That preg_replace statement throws an error: Parse error: syntax error, unexpected '/', expecting ')' – Stijn Hoste Jun 19 '13 at 19:19
  • Glad to help; I updated my comment before I saw yours. `str_replace` only handles the single quote case - you might want a `preg_replace` if there are other characters you need to handle – Hobo Jun 19 '13 at 19:22
  • Sorry, I've been away. The problems with typing code without being able to test. I think I missed the quotes around the regex: `preg_replace('/&[^;]+;/', '', $page_title);`. – Hobo Jun 19 '13 at 20:35
0

Try preg_replace('/[^A-Za-z0-9\-]/u', '', $page_title);

the u after the pattern processes unicode characters too.