I have an email address format like this - st15103@live.tees.ac.uk what i want to do is extract the ID part of this email like this - st15103 How can i do this using PHP string function ?
Asked
Active
Viewed 123 times
2
-
1This website is not a code writing service. Please research your problem and post what you have tried including any errors. – user4317867 Dec 25 '15 at 17:29
-
Hello user4317867, i don't know what's your problem. I know that. i'm asking the question because i don't know the solution for it. suddenly i found the solution that's why i posted my finding to the audience. – asela daskon Dec 25 '15 at 18:43
-
I don't have a problem, this website exists for answering errors in programming/scripting. Not a place to ask "I need to do X" without showing any research effort or code sample of what was tried. Key point being effort. I found a duplicate question [here](http://stackoverflow.com/questions/5941832/php-using-regex-to-get-substring-of-a-string) – user4317867 Dec 25 '15 at 18:50
2 Answers
3
Just split on @
and get the first part ?
$id = array_shift( explode("@", " - st15103@live.tees.ac.uk") );

adeneo
- 312,895
- 29
- 395
- 388
-
Thanks adeneo. I found the solution like this - strtok('st15103@live.tees.ac.uk', '@'); – asela daskon Dec 25 '15 at 17:28
-
Simply just select array key. But array_shift sounds good option. +1 – Utkarsh Dixit Dec 25 '15 at 17:30
-
Well, I guess `strtok` works as well, but my limited PHP knowledge is that `explode` is meant for splitting strings by strings, while `split` accepts a regex, and `strtok` creates tokens. – adeneo Dec 25 '15 at 17:33
2
It's Simple explode it.
<?php
$id = explode('@',t15103@live.tees.ac.uk)[0];
echo $id;
?>
Hope this helps you

Utkarsh Dixit
- 4,267
- 3
- 15
- 38