Jump to content

preg patten matching


StirCrazy

Recommended Posts

Hey all :D

 

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

<?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

thanks badbad :D

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.