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

What is happening when Matlab is starting a "parallel pool"? -

php - Cannot override Laravel Spark authentication with own implementation -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -