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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.