$re = '/^https:\/\/(?:www\.)?kuula.co\/share\/(?:[A-Za-z0-9]+\/)?(?:collection\/)?([A-Za-z0-9]+)(\?.*)?$/m';
$str = 'https://kuula.co
https://kuula.co/about
https://kuula.co/post
https://kuula.co/share
https://kuula.co/share/39u4$
https://kuula.co/post/7qQZ7
https://kuula.co/post/7qQZ7/collection/7lPZ8
http://kuula.co/share/7qQZ7
https://kuula.co/share/7qQZ7
https://kuula.co/share/collection/7lPZ8
https://kuula.co/share/7qQZ7?fs=1&abc=10
https://kuula.co/share/7qQZ7/collection/7lPZ8
https://kuula.co/share/collection/7lPZ8?vr=0
https://www.kuula.co/share/7qQZ7?abc=1
https://another-page.net/share/7qQZ7
https://kuula.co.another-page.com/share/7qQZ7';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php