douchujian8124 2018-08-08 10:19 采纳率: 100%
浏览 131
已采纳

从URL获取Instagram用户名 - 忽略带有正则表达式的句点

I've been able to retrieve the Username from an instagram profile URL with regex, however it stops once it reaches a full-stop

.

Full URL: https://www.instagram.com/username.test.uk/

The output from my regex: https://www.instagram.com/username

My regex and output:

// Regex for verifying an instagram URL
$regex = '/(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.com|instagr.am)\/([A-Za-z0-9-_]+)/im';

// Verify valid Instagram URL
if ( preg_match( $regex, $instagram_url, $matches ) ) {

    $instagram_username = $matches[1];

    var_dump($instagram_username);

The var dump produces: username

Any obvious changes needed to my regex to exclude . in the username section?

  • 写回答

1条回答 默认 最新

  • doujingdai5521 2018-08-08 10:22
    关注

    So it stops because your match for the username doesn't include the ..

    /(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.com|instagr.am)\/([A-Za-z0-9-_]+)/im
    

    This should probably include \. within ([A-Za-z0-9-_]+).

    /(?:(?:http|https):\/\/)?(?:www.)?(?:instagram.com|instagr.am)\/([A-Za-z0-9-_\.]+)/im
    

    This will capture all alpha-numerics, underscores and dots.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?