PHP regex to extract first part of file name in URL
extract, filenames, php, regex
Solution
$url = 'http://ecx.images-amazon.com/images/I/5104Xl51zFL._SL175_.jpg';
$path = parse_url($url, PHP_URL_PATH);
$filename = basename($path);
$partOne = strtok($filename, '.');
Problem
Possible Duplicate: How can I convert ereg expressions to preg in PHP? I'm working on a PHP script. I have a URL of the form: http://ecx.images-amazon.com/images/I/5104Xl51zFL._SL175_.jpg and I simply want to grab the first part of the file name `5104Xl51zFL`. I'm pretty new to regexps but so far I have: ``` .*images\/I\/(.+?)(\.[^.]*|$) ``` which according to regextester.com should work but doesn't in my PHP. Doesn't have to be a regexp if it's not the best solution. If it's relevant here's my PHP (still debuggy): ``` function linkExtractor($html) { if(preg_match_all('/<img ([^>]* )?src=[\"\']([^\"\']*\._SL175_\.jpe?g)[\"\']/Ui', $html, $matches, PREG_SET_ORDER)){ foreach ($matches as $match) { $url = $match[2]; echo "\n\n" .$url . "\nfile name: "; if(preg_match_all('.*images\/I\/(.+?)(\.[^.]*|$)', $url, $matched, PREG_SET_ORDER)) { foreach($matched as $name) { print_r($matched); } } } } } ```