正则表达式提取单词

时间:2015-10-08 14:15:53

标签: php regex

我的目标是从/photos/

中提取$file之后的用户名
$file = '/photos/plant_a/sort,"1,528","1,278",00:10:21,"1,263"
     /photos/forest-b/nosort,"1,528","1,278",00:10:21,"1,263"
     /photos/soil_c1/,"1,528","1,278",00:10:21,"1,263"
     /photos/jungle_c2,"1,528","1,278",00:10:21,"1,263"';

preg_match_all('~/photos/(.+?)/~', $file, $matches);
print_r($matches[1]);

此选项仅输出plant_aforest-bsoil_c1,但不输出jungle_c2

这些是用户名,可以包含_-和数字。一旦出现逗号,就意味着该URL的结尾。

如何提取juncle_c2

1 个答案:

答案 0 :(得分:3)

我认为这是有效的

inside findMyLocation * inside didUpdateLocations * Latitude: -26.204103 Longitude: 28.0473051inside sendDataToServer * inside didUpdateLocations * Latitude: -26.204103 Longitude: 28.0473051inside sendDataToServer * inside didUpdateLocations * Latitude: -26.204103 Longitude: 28.0473051inside sendDataToServer *

编辑(因为有关换行符的评论):~/photos/([^/]+)~

第二次编辑(自下次评论以来):~/photos/([^/\r\n]+)~