Jump to content

Matching all URLs except Soundcloud & Youtube ?


matmushroom

Recommended Posts

Hey guys.

 

I have a string like this:

 

<before>http://www.soundcloud.com/artist/track<after>
<before>www.facebook.com/page<after>
<before>www.youtube.com/watch?v=1234567<after>
<before>www.somesite.com/bla<after>
I created this RegEx to find and replace URLs with preg_replace
((https?:\/\/)(www\.)|(https?:\/\/)|(www\.))[^ <]+

...but how I extend that RegEx to match all URLs which are NOT Youtube or Soundcloud URLs.

 

Thanx for helping.

 

Mat


$list=<<EOL
<before>http://www.soundcloud.com/artist/track<after>
<before>www.facebook.com/page<after>
<before>www.youtube.com/watch?v=1234567<after>
<before>www.somesite.com/bla<after>
EOL;

preg_match_all('~<before>(?!.*(?:soundcloud|youtube))((?:https?://)?(?:www\.)?[^<]+)<after>~',$list,$urls);

print_r($urls);
/* output:
Array
(
[0] => Array
(
[0] => <before>www.facebook.com/page<after>
[1] => <before>www.somesite.com/bla<after>
)

[1] => Array
(
[0] => www.facebook.com/page
[1] => www.somesite.com/bla
)

)
*/
  • 2 weeks later...

Sorry, maybe I've explained it a big wrong. The <before> and <after> should mean that there is some random text.

 

So instead...

<before>http://www.soundcloud.com/artist/track<after>
<before>www.facebook.com/page<after>
<before>www.youtube.com/watch?v=1234567<after>
<before>www.somesite.com/bla<after>

...it could also be like this...

Blablabla http://www.soundcloud.com/artist/track
www.facebook.com/page is my page
Try www.youtube.com/watch?v=1234567 for my video
Check http://www.somesite.com/bla.

I would like to automatically insert different wordpress shortcodes inside a user generated post, exchange urls with videos or soundcloud widgets and create regular links from all the other URLs and emails.

 

Thanks for helping guys.

 

greetz,
 

Mat 

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.