1

I was trying to use preg_replace on a string but I obtain a wrong result.

$string = "Da venerdì 26 agosto a lunedì 5 settembre 2011";
$string = preg_replace('/\p{L}+/s','',$string);

should return " 26 5 2011 " but it returns " ¬ 26 ¬ 5 2011 "

*note in my local php server on Windows XP all works fine but in my remote php server on Debian it retunrs me the wrong string

Can you help me?

2
  • try adding i flag ... the ¬ symbol might be an uppercase letter :-D Commented Aug 31, 2011 at 11:48
  • 2
    This looks like you have a charset-problem in your input (ì looks like a multibyte-char, which is displayed as two different chars); use UTF-8 and set mbstring.func_overload = 7 in your php.ini to use the multibyte function-replacements in PHP. Commented Aug 31, 2011 at 11:50

1 Answer 1

2

You want to extract number from the string from what i see.

  • you have some encoding issues with your texts. Those chars should be UTF8, but they somehow got screwed up. Maybe you have them from the DB and the DB tables are not UTF8?
  • If you want to extract all numbers, then dont assume that a string is composed only from letters and numbers. Look at http://www.regular-expressions.info/unicode.html for a list of supported character types
  • use preg_replace("/[\s]+/", ' ', preg_replace('/[^0-9\s]/',' ',$string)); to remove anything non-number from the string, and leave only one separator space between numbers
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.