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

3
  • 2
    I'm not getting the same result you do: codepad.org/Ir5ZIsLk Jun 19, 2013 at 19:01
  • 3
    The is inserted using the character reference &#8217;.
    – Gumbo
    Jun 19, 2013 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... :) Jun 19, 2013 at 19:09

2 Answers 2

1

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

6
  • 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, 2013 at 19:12
  • I want the ' to be removed out of the string. Jun 19, 2013 at 19:14
  • You could use str_replace explicitly, replacing '&#8217;' 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, 2013 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 ')' Jun 19, 2013 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, 2013 at 19:22
0

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

the u after the pattern processes unicode characters too.

8
  • Thanks for your reply. But it doesn't work... It keeps returning "regio8217s" Jun 19, 2013 at 19:07
  • I assume you're viewing this output in a web browser, right? Do a view-source on those results and see if it's really the raw unicode character in there, or really the HTML-encoded version. Jun 19, 2013 at 19:14
  • You may need to do an additional replacement specifically to look for and replace HTML-encoded unicode characters, like preg_replace('/&#[0-9]+;/g', '', $page_title); Jun 19, 2013 at 19:15
  • It outputs this in the browser: <div id="menu"> <ul> regio&#8217;s <br/>regio8217s </ul> </div> Jun 19, 2013 at 19:16
  • 1
    Ah yes, PHP's preg_replace is global by default, so there's no need for the "g" after the pattern. Jun 19, 2013 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.