StirCrazy Posted March 18, 2008 Share Posted March 18, 2008 Hey all Having a little problem with pattern matching (because I'm crap at it LOL). I need to remove a variable URL from page text. Problem is the URL is wedged between bbcode. The domain variable:- $removedomain = "domain.com"; Needs to cater for:- with or without http / https :// with or without www. EG: [url(.*) $domain (.*) [/url] $pagetext = (preg_replace($pattern, '', $pagetext)); Can anyone help me with the correct pattern to remove the right domain? Thanks, S.C> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 <?php $domain = 'domain.com'; $domain = str_replace('.', '\.', str_replace('-', '\-', $domain)); $pattern = '/(https?\:\/\/)?(www\.)?'.$domain.'\/?/i'; $pagetext = (preg_replace($pattern, '', $pagetext)); ?> Not tested. Try with a hyphen in the domain name too. Well, guess we have to escape hyphens too. Code edited. Quote Link to comment Share on other sites More sharing options...
StirCrazy Posted March 18, 2008 Author Share Posted March 18, 2008 thanks badbad Kinda got it working using below, but is a very dirty way of coding. There must be a patten that matches it. $message = (str_replace('[url]http://www.' . $domain, '', $message)); $message = (str_replace('[url=http://www.' . $domain, '', $message)); $message = (str_replace('[url="http://www.' . $domain, '', $message));[/code] I'll give your way a go and see what happens. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 Ah, I forgot the BB-code. Makes it a lot more difficult, considering [url]link[/url], [url="link"]text[/url] and [url=link]text[/url] could occur. Try $pattern = '/(\[url=?"?\]?)(https?\:\/\/)?(www\.)?'.$domain.'\/?"?\]?(.*?)?\[\/url\]/i'; I'm not an expert, so this would be some kind of a miracle if it actually works. Guess I'll better test it myself now.. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 Oh my God, I think it works Quote Link to comment Share on other sites More sharing options...
StirCrazy Posted March 18, 2008 Author Share Posted March 18, 2008 you my friend are a genius! Thank you very much for the help (sorry for the delay in replying, was feeding the baby). 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.