edainworks.com :: VGR :: testing strpos array function on email addresses to extract username.

On this set of email addresses :
$emailAddrs = array("namepart@domain.com","@onfirstpos","missing","",'');

Array ( [0] => namepart@domain.com [1] => @onfirstpos [2] => missing [3] => [4] => )
With this code :
foreach ($emailAddrs as $emailAddr ) {
  if (strpos($emailAddr,'@') > 0) {
   $name = substr($emailAddr,0,strpos($emailAddr,'@'));
  } else {
   $name = "Invalid";
  }   
  echo "'$emailAddr' ... '$name'
"; } // foreach

'namepart@domain.com' ... 'namepart'
'@onfirstpos' ... 'Invalid'
'missing' ... 'Invalid'
'' ... 'Invalid'
'' ... 'Invalid'

with >=
'namepart@domain.com' ... 'namepart'
'@onfirstpos' ... ''
'missing' ... ''
'' ... ''
'' ... ''

(VGR21082009 ADDed) with correct !==FALSE handling (see online manual)
'namepart@domain.com' ... 'namepart'
'@onfirstpos' ... ''
'missing' ... 'Invalid'
'' ... 'Invalid'
'' ... 'Invalid'

Vincent Graux (VGR) for European Experts Exchange and Edažn Works  back to list of test scripts
Last update 2024-05-11 14:44:42