Page 1 of 3

How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 3:37 am
by simonmlewis

Code: Select all

is_numeric (var_name)
I know this code checks if it is numeric, but what if I want to know if $ping has numeric content?

Code: Select all

$ping = "32ms";
is_numeric won't take that as being numeric. I need to know if there is numeric content *within* the variable.

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 3:54 am
by requinix
If you consider "ksjvdyfby3sdkhfb" to have numeric content then all you're doing is looking for a digit in there.

Code: Select all

preg_match('/\d/', $variable)

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 4:19 am
by simonmlewis
It doesn't matter.
I'm building a little tool where customers can ping their site, through ours. So they can see the server response time, without having to work out the CMD ... PING tool in Windows.
The result that comes back is like 32ms... but if the result is an error, I want to spot if it has numbers in it.

Code: Select all

<div class='width-narrow'>
<?php
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;

function ping($host, $port, $timeout) { 
  $tB = microtime(true); 
  $fP = fSockOpen($host, $port, $errno, $errstr, $timeout); 
  if (!$fP) { return "down"; } 
  $tA = microtime(true); 
  return round((($tA - $tB) * 1000), 0)." ms"; 
}

if (isset($domain))
{
$ping =  ping("$domain", 80, 10); 
}
echo "<div class='site-speed'>
<div class='site-speed-clock'>";
if (!isset($domain)) { echo "<i class='fa fa-clock-o' aria-hidden='true'></i>";}
if (isset($domain) && (1 === preg_match('~[0-9]~', $ping)))
{
if ($ping > 100) { echo "<i class='fa fa-frown-o' aria-hidden='true' style='color: #ff0000'></i>";}
if ($ping < 100 && $ping > 60) { echo "<i class='fa fa-meh-o' aria-hidden='true' style='color: #ff5500'></i>";}
if ($ping < 60) { echo "<i class='fa fa-smile-o' aria-hidden='true' style='color: #009900'></i>";}
}

echo "</div>";
if (!isset($domain))
{
echo "Enter your full URL of your website here and click Go.";
}

if (isset($domain) && (1 === preg_match('~[0-9]~', $ping)))
{
//Echoing it will display the ping if the host is up, if not it'll say "down".

echo "The speed Google pings $domain at is: <b>$ping</b>";
if ($ping > 100) { echo "<br/><font color='#ff0000'>This is rather slow, and slow sites are difficut for consumers, and Google will penalize you for it.</font>";}
if ($ping < 100 && $ping > 60) { echo "<br/><font color='#ff5500'>This is quite a good speed.<br/>No real issues, though could be quicker.<br/>Try a F5 to refresh to see if the speed reduces.</font>";}

if ($ping < 60) { echo "<br/><font color='#009900'>This is very good.  No issues - very fast!</font>";}
}
if (isset($domain) && (1 !== preg_match('~[0-9]~', $ping)))
{
echo "Sorry the domain you entered is either invalid, or the site you entered is down.";
}

echo "<form method='GET' action='index.php'>
<input type='hidden' name='page' value='website-speed'>
<input type='text' name='domain' ";
if (isset($domain)) { echo "value='$domain'";}
echo "><input type='submit' value='Go'></form></div>";
?><br/>
</div>
The other issue is that when you do an actual CMD Ping, it does it four times.
Trying to work out how I can make this function do it four times with four separate results in four variables.

Apart from literally duplicating the code and renaming it all... can you see a way to do that?
Then I could take the four values, get the average and give the result.

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 5:05 am
by Celauran
simonmlewis wrote:Trying to work out how I can make this function do it four times with four separate results in four variables.
You mean like a for loop?

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 5:12 am
by simonmlewis
Sure, but I would need the function to go into a loop, and then to store them into four variables... wouldnt' it?
Not sure how to do that.

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 5:36 am
by Celauran
Why four variables? Why not just use an array?

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 5:40 am
by Celauran
Alternately, you could pass the count as a parameter to the ping function and have the for loop inside the function.

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 5:50 am
by simonmlewis
Alternately, you could pass the count as a parameter to the ping function and have the for loop inside the function.
How...??
Also, I'm still not that familiar with Arrays.
This would also be a set of 4, like the cmd/ping is.

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 5:58 am
by Celauran
What if you did something like this instead?

Code: Select all

function ping($host, $count = 4, $port = 80, $timeout = 10) {
    $ping_times = [];

    for ($i = 0; $i < $count; $i++) {
        $start_time = microtime(true);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$fp) {
            throw new Exception('Not a valid hostname or domain is down');
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round((($end_time - $start_time) * 1000), 0);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 6:06 am
by simonmlewis
Ok so $count is set to 4. $i counts up to 4, and while it does that it loops through it.
The $response_time is put into the $ping_times array.
So how do I then extra it from that array, and get an average of the four?
Or.... extra them to show the four results, and calculated based on an average?

I assume something like $pingavg = avg($pine-array);

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 6:10 am
by Celauran
You could iterate over them using foreach. Average could be as simple as

Code: Select all

$pings = ping('domain.com');
$average = array_sum($pings) / count($pings);

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 6:16 am
by simonmlewis

Code: Select all

function ping($host, $count = 4, $port = 80, $timeout = 10) {
    $ping_times = [];

    for ($i = 0; $i < $count; $i++) {
        $start_time = microtime(true);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$fp) {
            echo "sorry this is not right";
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round((($end_time - $start_time) * 1000), 0);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}

if (isset($domain))
{
$ping =  ping("www.bbc.co.uk", 80, 10); 
$average = array_sum($ping_times) / count($pings);
}


echo "$average";
So this should show a result ??

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 6:18 am
by Celauran
That's going to try 80 times on port 10...

Also, $pings and $ping_times do not appear to be defined.

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 6:20 am
by Celauran

Code: Select all

<?php

function ping($host, $count = 4, $port = 80, $timeout = 10) {
    $ping_times = [];

    for ($i = 0; $i < $count; $i++) {
        $start_time = microtime(true);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

        if (!$fp) {
            throw new Exception('Not a valid hostname or domain is down');
        }

        $end_time = microtime(true);
        fclose($fp);
        $response_time = round(($end_time - $start_time) * 1000);
        $ping_times[] = $response_time;
    }

    return $ping_times;
}

$times = ping('www.bbc.co.uk');

echo "<p>Average response time: " . round(array_sum($times) / count($times)) . "ms</p>";

foreach($times as $time) {
    echo $time . "ms<br>";
}

Re: How do you check if a variable has numeric content?

Posted: Fri Feb 17, 2017 6:29 am
by simonmlewis
Gotcha. Clever. I wondered why it really churned before throwing a bunch of errors.
Only problem now is that if I enter "asdf" into a field and post it (_GET) to $domain, it throws a ton of errors, rather than throwing it out.