musclehead Posted October 26, 2007 Share Posted October 26, 2007 I need to search a string for a specific-size integer. For example, the string would be: "This is my string 123456789 and it contains an integer." In all my string values, the integers are all exactly 10 digits in length, and there are no other occurrences of this length integers. I would like to figure out a PHP function to search for any 10-digit-length integers in the string. Any ideas? Thank you all! Quote Link to comment https://forums.phpfreaks.com/topic/74868-searching-string-for-integer-length-help/ Share on other sites More sharing options...
GingerRobot Posted October 26, 2007 Share Posted October 26, 2007 Aside from the point that in your example, the integer does not have 10 digits, you'll need to use regular expressions: <?php $str = 'This is my string 1234567890 and it contains an integer'; preg_match_all('|\d{10,10}|',$str,$numbers); print_r($numbers); ?> Edit: I Misread that. If this number only occurs once, and you just want to check for it's existance, then something like this would work: <?php $str = 'This is my string 1234567890 and it contains an integer'; if(preg_match('|\d{10,10}|',$str,$numbers)){ echo '10 digit number found! The number was: '.$numbers[0]; }else{ 'No 10 digit number within this string!'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74868-searching-string-for-integer-length-help/#findComment-378523 Share on other sites More sharing options...
musclehead Posted October 26, 2007 Author Share Posted October 26, 2007 Thanks for the help...I failed to mention I need to replace those 10-digit (and sorry for only writing until 9...) integers with line breaks. I tried: $result = preg_replace("\d{10,10}","NEWENTRY",$string); But that appears to be bailing as well. I saw your edit post also and tried that, but it's only picking up one of the 10-digit integers: String: text text text 1231242523 text text text text text1313874376text text text text1232873298 The preg_match function is only detecting the first one, even within a loop. Quote Link to comment https://forums.phpfreaks.com/topic/74868-searching-string-for-integer-length-help/#findComment-378527 Share on other sites More sharing options...
musclehead Posted October 26, 2007 Author Share Posted October 26, 2007 Sorry for all the posts - I've got it working now! $newstr = preg_replace("|\d{10,10}|","<br />",$str); That does the trick and replaces all occurrences of the integer w/ a line break. I was stuck on the formatting of the search string. Thanks very much for your help!! Quote Link to comment https://forums.phpfreaks.com/topic/74868-searching-string-for-integer-length-help/#findComment-378530 Share on other sites More sharing options...
kellz Posted October 26, 2007 Share Posted October 26, 2007 this does exactly what you asked for: function MyFunction($str) { $newstr = ''; $x = split(" ", $str); for ($i=0;$i < sizeof($x);$i++){ if (strlen($x[$i]) == 9 && preg_match("/[0-9]/", $x[$i])) $x[$i]="New_Value"; //Change "New_Value" to whatever you want to replace the 9 digit numbers with. $newstr .= $x[$i] . "\r\n"; } return $newstr; } echo MyFunction("This is my string 123456789 and it contains an integer."); edit: ok lol you done it yourself but i'll leave this post for other people.. Quote Link to comment https://forums.phpfreaks.com/topic/74868-searching-string-for-integer-length-help/#findComment-378698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.