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 Quote Link to comment 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> Quote Link to comment 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/'); Quote Link to comment Share on other sites More sharing options...
hmm213 Posted December 11, 2007 Author Share Posted December 11, 2007 k thanks guys Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.