shashank
shashank

Reputation: 25

Extracting value in php

I have a string like this "firstname lastname(email)" and I want to extract "email" from this string in php. What should I do? any solution...

Upvotes: 0

Views: 71

Answers (4)

Er. Anurag Jain
Er. Anurag Jain

Reputation: 1793

try code

 $str = "firstname lastname(email)"; //Given string 
 $begin =  strpos($str , "(") + 1;    //string position of first letter of email
 $length =  strpos($str , ")") - $begin; // Total length of email

 echo substr($str , $begin  , $length );  // using substr php function

thanks

Upvotes: 0

benedict_w
benedict_w

Reputation: 3608

Using a regular expression and preg_match()?

e.g. matches anything in brackets:

preg_match ('/\((.+)\)/', $subject, $matches);

e.g. or parse for an email:

preg_match ('/^([\w\.-]{1,64}@[\w\.-]{1,252}\.\w{2,4})$/', $subject, $matches);

Upvotes: 1

user1421727
user1421727

Reputation:

Use this function preg_match()

preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $email);

The variable $email[0] holds the email extracted from the variable $string. If you have many such emails increment in index of the array variable $email.

Upvotes: 1

case1352
case1352

Reputation: 1136

where

$str = "firstname lastname(email)";

try

$email = substr($str, stripos($str,"(")); // still has closing ) 
$email = substr($email, 0,strlen($email)-1); // removes )

Upvotes: 0

Related Questions