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 /
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; my conclusion
regex not suitable type of job it's way slow such simple substring pattern.


Comments
Post a Comment