matmushroom Posted March 12, 2014 Share Posted March 12, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/286908-matching-all-urls-except-soundcloud-youtube/ Share on other sites More sharing options...
.josh Posted March 12, 2014 Share Posted March 12, 2014 $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 ) ) */ Quote Link to comment https://forums.phpfreaks.com/topic/286908-matching-all-urls-except-soundcloud-youtube/#findComment-1472313 Share on other sites More sharing options...
matmushroom Posted March 21, 2014 Author Share Posted March 21, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/286908-matching-all-urls-except-soundcloud-youtube/#findComment-1473427 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.