2

I've this simple code:

function getCleanText($rawText) //removes doublespace and punctuation
{
    return strtolower(preg_replace("/[\s\t]+/u", " ", 
        preg_replace("/[^a-zA-Z1-9àèéìòù]+/u", " ", $rawText)));
}

echo getCleanText("uscì"). " uscì <br>";

the function just removes punctuation and double spaces. Why i've this output?

usc�� uscì 

I mean "uscì" doesn't have any punctuation and the function is supposed to return it as it is without modification. Still i've problem with all accented letters. The web page is encoded in UTF-8. if i try with utf_encode like this

return utf8_encode(strtolower(preg_replace("/[\s\t]+/u", " ", 
        preg_replace("/[^a-zA-Z1-9àèéìòù]+/u", " ", $rawText))));

the output is

usc㬠uscì 

any ideas? Where i can find some documentation to understand my error?

4
  • Not getting that behavior here, 3v4l.org/n652S; eval.in/459413. You sure the input is as you have it here? Your file is utf8? Commented Oct 29, 2015 at 15:58
  • with or without ini_set the result is the same. The source file is encoded in utf-8. Commented Oct 29, 2015 at 16:03
  • did you check return strtolower(preg_replace("/[\s\t]+/u", " ", preg_replace("/[^a-zA-Z1-9àèéìòù]+/u", " ", utf8_encode($rawText)))); ? Commented Oct 29, 2015 at 16:12
  • actually the accented characters work fine: echo "uscì" outputs as expected. the @JulioSoares solution outputs usc uscì Commented Oct 29, 2015 at 16:13

1 Answer 1

1

Using mb_strtolower, rather than just strtolower resolves the problem in my tests. I assume it's a php.ini configuration issue that's means it works OK for some people and not others.

Sign up to request clarification or add additional context in comments.

1 Comment

return mb_strtolower(preg_replace("/[\s\t]+/u", " ", preg_replace("/[^a-zA-Z1-9àèéìòù]+/u", " ", $rawText)), 'UTF-8'); works! For some reason i was sure that the problem was in the preg_replace function. Thanks!

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.