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> Link to comment https://forums.phpfreaks.com/topic/96671-preg-patten-matching/ 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. Link to comment https://forums.phpfreaks.com/topic/96671-preg-patten-matching/#findComment-494714 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. Link to comment https://forums.phpfreaks.com/topic/96671-preg-patten-matching/#findComment-494729 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.. Link to comment https://forums.phpfreaks.com/topic/96671-preg-patten-matching/#findComment-494744 Share on other sites More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 Oh my God, I think it works Link to comment https://forums.phpfreaks.com/topic/96671-preg-patten-matching/#findComment-494757 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). Link to comment https://forums.phpfreaks.com/topic/96671-preg-patten-matching/#findComment-494776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.