0

Here, I am using strstr to remove after characters and substr is used to remove before character. But how to remove all characters before and after at once ?

I need to get only 80 from this variable $mrk = 'Science-80_John' omitting Science and John.

What I am doing

$mrk = 'Science-80_John';

$mark = substr($mrk,strpos($mrk,'-')+1);
//output: 80_John

echo strstr($mark, '_', true);
//output: 80

Any other short or best way ?

Dipak
  • 931
  • 14
  • 33
  • This is a duplicate of https://stackoverflow.com/questions/6278296/extract-numbers-from-a-string – hellork Nov 22 '18 at 18:57

2 Answers2

2

Since op asked for a short or best way. Number_from_string might perform better using a cast. Ignoring minus signs (which could be hyphens in this example), we get a positive integer.

>>> $mrk = 'Science-80_John';
=> "Science-80_John"
>>> $num = (int)strpbrk ($mrk, "0123456789");
=> 80

The above use case assumes, besides only extracting one positive integer, that scientific notation is OK. John has decided to be a clever lad and change his signature to a very large number. We'd have to decide under what circumstances the system should accept or reject scientific notation.

>>> $mrk = 'Science-80E+21_John45';
>>> $num = (int)strpbrk ($mrk, "0123456789");
=> 9223372036854775807 // ack!

>>> $pat = "0123456789";
>>> $num = strpbrk ($mrk, $pat);
>>> $num = substr($num,0 ,strspn($num, $pat));
=>  "80"

Since the new return value consists of digits, we don't strictly need the cast anymore because of PHP's type juggling. If there are no numbers at all, this solution returns the empty string, "" which will generate an error if used in calculations later. Casting to (int) would silently return 0 but not tell us if the database needs correction. Which would work better in this situation? Another design decision.

String functions also avoid potential pitfalls with regex in some solutions from Extract numbers from a string. What happens if there is an extra "45" in $str? The result, 8045 is probably not desirable.

>>> $mrk = 'Science-80_John45';
=> "Science-80_John45"
>>> $newStr = preg_replace("/\D+/", "", $mrk);
=> "8045"
hellork
  • 324
  • 2
  • 8
  • Great ! better than regex !! – Dipak Nov 22 '18 at 19:24
  • 1
    Note that if somebody later views this answer and needs negative numbers, they'll have to add the `-` character to the `strpbrk` pattern. Here, that would not work because there is no easy way to distinguish between `minus signs` and hyphens. We'll leave that for another time. – hellork Nov 22 '18 at 19:51
1

You can use regex in preg_replace() and remove add non-digit characters

$mrk = 'Science-80_John';
$newStr = preg_replace("/\D+/", "", $mrk);
// 80
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • What about performance. Is it best to use regex or the previous way `substr` and `strstr`. Can you share the idea ? – Dipak Nov 22 '18 at 19:00
  • In this case using string function is better than regex. Check [Which is more efficient, PHP string functions or regex in PHP?](https://stackoverflow.com/questions/634676/which-is-more-efficient-php-string-functions-or-regex-in-php) – Mohammad Nov 22 '18 at 19:04