regular expression for string comparssion using php

前端 未结 2 1849
自闭症患者
自闭症患者 2021-01-26 17:06

I am new to regex in php, I am trying to get the values between @ and . in a string. for example if a string contains value abc@gmail.com

相关标签:
2条回答
  • 2021-01-26 17:40
    $str = 'abc@gmail.com';
    
    preg_match('/@([^.]+)/', $str, $match);
    
    echo $match[1]; // gmail
    

    Breakdown of above:

    • @ start search from the character @
    • [^.]+ match 1 or more characters that are not the character .
    • The ( ) is to capture that portion in a backreference which in this case would be the index 1
    • So we access it through $match[1]
    0 讨论(0)
  • 2021-01-26 17:56

    Simple as mentions above :

    $input=hardiksondagar@gmail.com;
    // now you want to fetch gmail from input user PHP's inbuilt function 
    preg_match('/@([^.]+)/', $input, $output);
    echo $output[1]; // it'll print "gmail"
    
    • Documentation of function : preg_match()
    0 讨论(0)
提交回复
热议问题