hmm213 Posted December 11, 2007 Share Posted December 11, 2007 OK, so I need a regex that will chop the / off the end of a domain name ( if it exists ), leaving the rest of the domain intact... mydomain.com/ -> mydomain.com mydomain.com -> mydomain.com mydomain.com/myfile/ -> mydomain.com/myfile etc.... I think you get the picture So far I have this ... preg_match('/(.*)[\/]+$/s',$google_text,$matches); $matches[1]; The problem is though - it leaves "domain.com/myfolder/myfile" blank ... Anyone got any suggestions ? THanks Link to comment https://forums.phpfreaks.com/topic/81112-annoying-regex/ Share on other sites More sharing options...
effigy Posted December 11, 2007 Share Posted December 11, 2007 <pre> <?php $tests = array( 'mydomain.com/', 'mydomain.com', 'mydomain.com/myfile/', ); foreach ($tests as $test) { echo "$test => ", preg_replace('%/\z%', '', $test); echo '<br>'; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/81112-annoying-regex/#findComment-411933 Share on other sites More sharing options...
BenInBlack Posted December 11, 2007 Share Posted December 11, 2007 not to denigrate effigy response, his relies on the / being at the end, i wondered if you wanted a more global approach haystack this is a test mydomain.com/ with diff mydomains some with slashes some without mydomain.com and some with domain.com/myfile/ regex /([\/])(?![[:alpha:]])/ result this is a test mydomain.com with diff mydomains some with slashes some without mydomain.com and some with domain.com/myfile code $output = preg_replace('/([\/])(?![[:alpha:]])/','','this is a test mydomain.com/ with diff mydomains some with slashes some without mydomain.com and some with domain.com/myfile/'); Link to comment https://forums.phpfreaks.com/topic/81112-annoying-regex/#findComment-412127 Share on other sites More sharing options...
hmm213 Posted December 11, 2007 Author Share Posted December 11, 2007 k thanks guys Link to comment https://forums.phpfreaks.com/topic/81112-annoying-regex/#findComment-412219 Share on other sites More sharing options...
manixrock Posted December 12, 2007 Share Posted December 12, 2007 Your regexp was good the problem was you put the "0 or more" quantifier ("*") which is why you've been getting empty strings sometimes. But this is easily solved in your case by anchoring the string at the beggining with "^" (you've anchored it at the end with "$"). It should look like this: /^(.*)[\/]+$/s Link to comment https://forums.phpfreaks.com/topic/81112-annoying-regex/#findComment-413035 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.