Hi, I am having the following function: function findinside($start, $end, $string) { preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m); return $m[1]; } PHP: In the file i have: $start = "/product/"; $end = ".html"; $string="http://domaincom/product/product-name.html"; PHP: When i call $out = findinside($start, $end, $string); PHP: It works with no problem and i get "product-name" But if $string contains ( or ) it stops working : $string="http://domaincom/product/product(best)-name.html"; PHP: Any idea on fixing it? Thanks
This might not help you with figuring out regular expressions, but in this case I don't believe you need to use one. PHP already has a function to return the file name: $string = 'http://domaincom/product/product(best)-name.html'; echo basename($string); // product(best)-name.html echo basename($string, '.html'); // product(best)-name PHP:
Yes but the problem is that i use preg_match_all as i have about 20 links/page that needs detected this way. So, in my case, $string can contain the source of an entire webpage.
function base_match_all($arr) { $all_your_base_are_belong_to_us = array(); foreach($arr as $string) { $all_your_base_are_belong_to_us[] = basename($string, '.html'); } return $all_your_base_are_belong_to_us; } $arr_string = base_match_all($strings); Code (markup): Maybe it is a bit slower, but it works just the same. Anyhow the preg_match_all problem might be because the ( ) needs to be back slashed and it is thinking (.*?) set type instead of exact string that needs to be found. So, it could be that preg_quote is not working like it should. hrm.. Think of changing ([^\.)]+) to something like (.*?) to match everything between?