first letter of lastname php


/ Published in: PHP
Save to your folder(s)

Get the first char of a lastname in php


Copy this code and paste it in your HTML
  1. <?php
  2. $mystring = 'Test Name';
  3. $findme = ' ';
  4. $pos = strpos($mystring, $findme);
  5.  
  6. // The !== operator can also be used. Using != would not work as expected
  7. // because the position of 'a' is 0. The statement (0 != false) evaluates
  8. // to false.
  9. if ($pos !== false) {
  10. $ln = substr($mystring, $pos, 2);
  11. echo "Last name starts with ". trim($ln, " ")."";
  12.  
  13. } else {
  14. echo "The string '$findme' was not found in the string '$mystring'";
  15. }
  16. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.