Which is more efficient, PHP string functions or regex in PHP?

后端 未结 9 1844
甜味超标
甜味超标 2020-12-06 16:48

I\'m writing PHP code to parse a string. It needs to be as fast as possible, so are regular expressions the way to go? I have a hunch that PHP string functions are more expe

9条回答
  •  有刺的猬
    2020-12-06 17:31

    If what you're doing is at all reasonable to do using string functions, you should use them. Like, if you're determining whether a constant string 'abc' occurs in $value, you definitely want to check strpos($value, 'abc') !== false, not preg_match('/abc/', $value). If you find yourself doing a lot of string reshuffling and transformations in order to accomplish what you would've with a regex, though, you're almost certainly going to wind up destroying both performance and maintainability.

    When concerned about speed, though, when it comes down to it, don't think about it, clock it. The time command is your friend.

提交回复
热议问题