note 118905 added to function.strpos
| From: | fabio at naoimporta dot com | Date: | Thu, 25 Feb 2016 16:58:51 +0000 |
| Subject: | note 118905 added to function.strpos | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205042@lists.php.net to get a copy of this message | ||
It is interesting to be aware of the behavior when the treatment of strings with characters using
different encodings.
<?php
# Works like expected. There is no accent
var_dump(strpos("Fabio", 'b'));
#int(2)
# The "á" letter is occupying two positions
var_dump(strpos("Fábio", 'b')) ;
#int(3)
# Now, encoding the string "Fábio" to utf8, we get some "unexpected" outputs.
Every letter that is no in regular ASCII table, will use 4 positions(bytes). The starting point
remains like before.
# We cant find the characted, because the haystack string is now encoded.
var_dump(strpos(utf8_encode("Fábio"), 'á'));
#bool(false)
# To get the expected result, we need to encode the needle too
var_dump(strpos(utf8_encode("Fábio"), utf8_encode('á')));
#int(1)
# And, like said before, "á" occupies 4 positions(bytes)
var_dump(strpos(utf8_encode("Fábio"), 'b'));
#int(5)
----
Server IP: 72.52.91.14
Probable Submitter: 177.205.100.168
----
Manual Page -- http://php.net/manual/en/function.strpos.php
Edit -- https://master.php.net/note/edit/118905
Del: integrated -- https://master.php.net/note/delete/118905/integrated
Del: useless -- https://master.php.net/note/delete/118905/useless
Del: bad code -- https://master.php.net/note/delete/118905/bad+code
Del: spam -- https://master.php.net/note/delete/118905/spam
Del: non-english -- https://master.php.net/note/delete/118905/non-english
Del: in docs -- https://master.php.net/note/delete/118905/in+docs
Del: other reasons-- https://master.php.net/note/delete/118905
Reject -- https://master.php.net/note/reject/118905
Search -- https://master.php.net/manage/user-notes.php