March 5, 201412 yr Hi, This one has me stumped. What I need to do is work out if a string contains numbers in a row longer than 6 characters. I'm toying with is_numeric and string length but I can't fathom it. Here is a string; $str = "Here is some text which contains numbers like this 123 4567 or could be like this 1234567 but these cheeky little numbers could appear anywhere but will always be in a row."; if(is_numeric($str)) { echo "I can has numbers!"; } else { echo"No Numbers"; } So this tells me that the sting has numbers. Now i'm trying to work out how to reach those numbers and then; $extract = "1234 567"; $extract = preg_replace("/\s+/", "", $extract); if(strlen($extract) >= 6) { echo "This string is greater than or equal to 6 numbers."; } else { echo "This string is 5 characters or less."; } So what I'm asking is what would be the middle process for this? How would I get the value of $extract?
March 5, 201412 yr I like the approach of removing everything but numbers, keeps it nice and simple so your only working with what you actually want to work with.
March 5, 201412 yr Author Hi, I'll tell you how I may approach it but then leave you to work out the code yourself, because it'll be fun for you. First approach: 1. Preg_replace all characters in the string that aren't numbers with nothing so that you are left with a string of numbers. 2. Check the variable matches what you want. Second approach: 1. Split the string into characters and put them in an array 2. Loop through the characters array and check to see if they are numeric. If they are, append to a variable. 3. Check the variable matches what you want Hope that helps? 100% on the work it out myself line. Option 1 This would make $str = "123 4567 1234567" which looks like 3 numbers or $str = "12345671234567". The issue I found whilst toying with this is although this string is over 6 numbers it would also catch $str = "Here is a string where 12345 is ok but 123456 is not". Option 2 I gave the following a run. $str = "Here is some text which contains numbers like this 123 4567 or could be like this 1234567 but these cheeky little numbers could appear anywhere but will always be in a row."; if(preg_match_all("/\d+/", $str, $found)) { print_r($found); } else { echo "No number found"; } This makes an array as follows; [0] => 123 [1] => 4567 [2] => 1234567 [3] => 12345 But because of the spaces it did not group 123 4567 into a single number and thus it not pass the string length test. However if trim the $str to start is finds 0 matches. Stumped but will keep thinking.
March 5, 201412 yr Author Hmm but then that makes all of them 6 characters which misses the point ... I'm going to try replacing non numbers with spaces and then see if I can combine units where there is only 1 space between them ... I can't think of another way.
March 5, 201412 yr Author Aha! $str = "Here is some text which contains numbers like this 123 4567 or could be like this 1234567 but these 300 cheeky little numbers like 12 345 6 could appear anywhere 2003 but will always be in a row. We don't want to catch low numbers like 30."; $str = str_replace(" ", "", $str); if(preg_match_all("/\d+/", $str, $found)) { $i = 0; foreach($found[$i] as $key => $extract) { if(strlen($extract)>=6) { echo "<b style='color: red'>" . $extract . "</b> has too many numbers in a row.<br>"; } else { echo $extract . " has less than 6 numbers in a row.<br>"; } $i++; } } Woohoo!
Create an account or sign in to comment