-1

I know this type of questions have been asked before but none solves my problem.

  1. I want to capture numeric parts of this string INFORMATICS&SYSTEMS-58600 i.e. 58600.
  2. I am trying to do substr(INFORMATICS&SYSTEMS-58600,-5) which returns ATICS which is substr of first part of string INFORMATICS but I want the last part.
  3. Wherever & is appearing this is behaving same.

I know its a very basic mistake but what ??? I cant figure out.Please help me out.

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
sandeep yadav
  • 107
  • 10

3 Answers3

1
$str = 'INFORMATICS&SYSTEMS-58600';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);

Can refer Extract numbers from a string

Community
  • 1
  • 1
test
  • 464
  • 5
  • 14
1

Actully PHP substr is working fine. 1. I was passing this text as url query in ajax i.e. get_data.php?dept ='informatics& system' so anything after & was treated as second parameter.

  1. I found this nice answer on link to pass ajax parameters in url as encoded.
Community
  • 1
  • 1
sandeep yadav
  • 107
  • 10
0

The regex in this code matches a number at the end of a string.

<?php
  $str = "INFORMATICS&SYSTEMS-58600";
  $matches = array();
  preg_match("/\d+$/", $str, $matches);
  foreach($matches as $match) {
    echo $match;
  }
?>

Output:

58600
Jason
  • 2,725
  • 2
  • 14
  • 22