Skip to content

Instantly share code, notes, and snippets.

@purarue
Last active February 14, 2020 18:32
Show Gist options
  • Save purarue/73833740051aa4444699957b498f2736 to your computer and use it in GitHub Desktop.
Save purarue/73833740051aa4444699957b498f2736 to your computer and use it in GitHub Desktop.
<?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