1

im trying to get any input-ed data to be replaced by the value '7', if it is not a number between 0 and 9.

Here's my code atm:

echo "Enter no. of days to search: ";
$handle = fopen ("php://stdin","r");
$days = fgets($handle);

$days2 = str_replace("\n", '', $days);

if ($days2 == '/^[0-9]{1}$/'){
    $days2 = "7";}

it doesnt replace any input (letters or numbers) with 7 though! Help please! Thanks

Edit:

if ($days2 >= 0 && $days2 <= 9){
    }
else $days2 = "7";

works but only for numbers outside the range, not for if i input letters

2

1 Answer 1

3
<?php
 if (!is_numeric($days2) || $days2 > 9 || $days2 < 0){
     $days2 = "7";
 }
?>

This might be what you're looking for, without using any regex

Sign up to request clarification or add additional context in comments.

3 Comments

No prob, dont forget to put your question as resolved ;)
just click the "check" symbol on the left of my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.