I just want to known which one will be fast of strpos()/stripos() or preg_match() functions in php.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Benchmarking is a tricky business, but it's fairly safe to say that
preg_match
is slower thanstrpos
orstripos
. This is because the PRCE functions implement a REGEX engine that is much more powerful and flexible than the string functions.They also do different things.
strpos
will tell you the index of the start of the string inside another string, whereaspreg_match
is mainly used to probe the format of a string, and to retrieve sections of it based on regular expressions.In short, if you simply want to find a string inside another string, use
strpos
orstripos
.preg_match
is the slowest of the three.stripos
will be slower thanstrpos
, since it will have to do extra work to handle case-insensitive matching.I found this blog that has run some testes regarding your question, the result was:
The code used was: