Extract numbers after the last hyphen in PHP?


Let’s say the following is our string including hyphen and numbers as well −

"John-45-98-78-7898906756"

To extract numbers after – i.e. hyphen, use the concept of explode() with parameters - and $yourVariableName.

The PHP code is as follows −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
   $values = "John-45-98-78-7898906756";
   $values = explode("-",$values);
   $values = $values[4];
   echo $values;
?>
</body>
</html>

Output

7898906756

Updated on: 20-Nov-2020

591 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements