Jump to content

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 

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.