regex - By using PHP preg_match, how to get specific substring from string -


i substring:

myphotoname 

from below string:

path/myphotoname_20170818_111453.jpg 

using php preg_match function.

please may me?

thank you.

preg_match / _.

$str = "blur/blur/myphotoname_20170818_111453.jpg"; preg_match("/.*\/(.*?)_.*(\..*)/", $str, $match);  echo $match[1] . $match[2]; 

i use .*? match between slash , underscore lazy make sure doesn't match way last underscore.
edited make greedy match before /

https://3v4l.org/9otuj

performance of regex:
enter image description here


since it's such simple pattern can use normal substr strrpos.
edited use strrpos last /

$str = "blur/blur/myphotoname_20170818_111453.jpg"; $pos = strrpos($str, "/")+1; // position of / $str = substr($str, $pos, strpos($str, "_")-$pos) . substr($str, strpos($str, ".")); // ^^ substring pos underscore , add extension  echo $str; 

https://3v4l.org/d411c

performnce of substring:
enter image description here

my conclusion
regex not suitable type of job it's way slow such simple substring pattern.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -