Match all youtube links in a string of text I'm looking to pull all the youtube links from a string of text and was wondering how does this look?

if (preg_match_all('/(https?:\/\/)?(www\.)?(youtube\.com)\/watch\?v=([a-zA-Z0-9_-]+)[^\s]*/im', $this-&gt;content, $matches)) {

}


Obviously this doesn't take into account the youtu.be links as they are formatted differently.
[^\s] is effectively just \S
For checking against youtu.be links, you can have:

youtu(\.be|be\.com)

If you do not want to store every matched group, use (?:...)
[A-Za-z0-9_] can be simplified to \w.
Use a different delimiter than / so that you won't have to escape it in pattern.


Therefore:

preg_match_all(
    '@(https?://)?(?:www\.)?(youtu(?:\.be/([-\w]+)|be\.com/watch\?v=([-\w]+)))\S*@im',
    $this-&gt;content,
    $matches
)