Last active
February 14, 2020 18:32
-
-
Save purarue/73833740051aa4444699957b498f2736 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getScoredBy($scoredBy): | |
{ | |
$scoredByNum = str_replace( | |
[',', ' users', ' user'], | |
'', | |
$scoredBy | |
); | |
if (!is_numeric($scoredByNum)) { | |
return null ; | |
} | |
return (int) $scoredByNum; | |
} | |
var_dump(getScoredBy("- user")); | |
var_dump(getScoredBy("- users")); | |
var_dump(getScoredBy("N/A users")); | |
var_dump(getScoredBy("0 users")); | |
var_dump(getScoredBy("1,200 users")); | |
var_dump(getScoredBy("500 users")); | |
var_dump(getScoredBy(" users")); | |
/* | |
OUTPUT: | |
NULL | |
NULL | |
NULL | |
int(0) | |
int(1200) | |
int(500) | |
NULL | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment