garyed Posted July 14, 2022 Share Posted July 14, 2022 I want to search for a string like: "Joe has 5 large apples" but don't know the actual number. In other words I want to search for the whatever number is in between "Joe has" & "large apples" I assume there is a way to do that in php but I haven't been able to find it. If so can someone help me with the solution ? Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/ Share on other sites More sharing options...
mac_gyver Posted July 14, 2022 Share Posted July 14, 2022 https://www.php.net/manual/en/function.preg-match.php Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598228 Share on other sites More sharing options...
garyed Posted July 14, 2022 Author Share Posted July 14, 2022 I've tried preg_match, preg_match_all & preg_quote but I can't find the syntax where I can find an unknown value in the string. All the examples I've found assume there are no unknowns in the string. In my string "Joe has 5 large apples", the number 5 is unknown during the programming call & I only know the string is "Joe has * large apples" . Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598229 Share on other sites More sharing options...
Barand Posted July 14, 2022 Share Posted July 14, 2022 Alternative "sans-regex" solution... $str = "Joe has 5 large apples"; echo current( array_filter(explode(' ', $str), 'is_numeric' ) ) ; // 5 Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598230 Share on other sites More sharing options...
Solution mac_gyver Posted July 14, 2022 Solution Share Posted July 14, 2022 $string = 'Joe has 5 large apples'; // find any digit(s) in the string if(preg_match('!\d+!', $string, $matches)) { print_r($matches); // result in $matches[0] } else { echo 'no number found'; } // find digit(s) with the specific before/after text if(preg_match('!Joe has (\d+) large apples!', $string, $matches)) { print_r($matches); // result in $matches[1] } else { echo 'no number found'; } Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598231 Share on other sites More sharing options...
garyed Posted July 14, 2022 Author Share Posted July 14, 2022 Thank you, that's what I was missing. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598232 Share on other sites More sharing options...
garyed Posted July 14, 2022 Author Share Posted July 14, 2022 Well I thought everything was solved but i found that (\d+) only works on whole numbers. I didn't think it would matter if the number had a few decimal points but I was obviously wrong. If there is a decimal like 5.5 or 5.25 then it will only pick up the 5 & not the rest of the number. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598236 Share on other sites More sharing options...
Barand Posted July 14, 2022 Share Posted July 14, 2022 Try my solution with 5.25 Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598237 Share on other sites More sharing options...
garyed Posted July 14, 2022 Author Share Posted July 14, 2022 13 minutes ago, Barand said: Try my solution with 5.25 Your solution would work if I knew the number in the string but the problem is that don't know the number that will be in the the string that I'm searching for. Basically I'm searching for the number inside a specific string that is inside a larger string. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598238 Share on other sites More sharing options...
Barand Posted July 14, 2022 Share Posted July 14, 2022 46 minutes ago, garyed said: Your solution would work if I knew the number in the string but the problem is that don't know the number that will be in the the string that I'm searching for. Bullsh*t. Have you even tried it? Example... $strings = [ "Joe has 5 large apples", "Joe has 5.25 large apples", "Joe has 500 large apples" ]; foreach ($strings as $str) { $number = current( array_filter(explode(' ', $str), 'is_numeric' ) ) ; echo "$str | $number<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598241 Share on other sites More sharing options...
garyed Posted July 15, 2022 Author Share Posted July 15, 2022 1 hour ago, Barand said: Bullsh*t. Have you even tried it? Example... $strings = [ "Joe has 5 large apples", "Joe has 5.25 large apples", "Joe has 500 large apples" ]; foreach ($strings as $str) { $number = current( array_filter(explode(' ', $str), 'is_numeric' ) ) ; echo "$str | $number<br>"; } I appreciate your help but I don't think that solves the problem. I just need to find one specific unknown number in a specific string out of a huge string with multiple numbers. The example of "Joe has (x) large apples" would be the line I'm searching for in like 1,000 lines of code with multiple numbers where (x) is an unknown number. Mac_gyver's solution worked because it took the specific string where the unknown number was inside it & pulled it out. The only problem is that it would only pull out a whole number & the decimal part was lost. Your solution would come up with too many numbers that I would still have to find a way to narrow it down to the specific string that holds the correct number. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598247 Share on other sites More sharing options...
kicken Posted July 15, 2022 Share Posted July 15, 2022 What you should do is spend a little time learning regular expressions, and play with them to find a working pattern. The pattern \d matches only digits (ie, 0 - 9). As such, it'll only match numbers the consist of simply digits (no decimals, no grouping characters). If you want to match decimals, you'd need to add . to the list of allowed characters. You can do that by specifying a set of possible characters using [ ]. Joe has ([\d.]+) large apples If you needed groups, you could likewise add , to the set. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598249 Share on other sites More sharing options...
mac_gyver Posted July 15, 2022 Share Posted July 15, 2022 @garyed, this is why you must address all the actual extents and limits of a problem, upfront, so that you don't waste time on partial solutions. it's not possible to sneak up on programming, because programming is like a BS detector. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598250 Share on other sites More sharing options...
garyed Posted July 15, 2022 Author Share Posted July 15, 2022 2 hours ago, kicken said: What you should do is spend a little time learning regular expressions, and play with them to find a working pattern. The pattern \d matches only digits (ie, 0 - 9). As such, it'll only match numbers the consist of simply digits (no decimals, no grouping characters). If you want to match decimals, you'd need to add . to the list of allowed characters. You can do that by specifying a set of possible characters using [ ]. Joe has ([\d.]+) large apples If you needed groups, you could likewise add , to the set. Thank you, That was what I needed & I definitely do need to spend a lot more time learning those expressions. I know it's wrong but I usually learn backwards & try to find a solution to a problem when i run into one instead of learning the basics first. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598251 Share on other sites More sharing options...
garyed Posted July 15, 2022 Author Share Posted July 15, 2022 1 hour ago, mac_gyver said: @garyed, this is why you must address all the actual extents and limits of a problem, upfront, so that you don't waste time on partial solutions. it's not possible to sneak up on programming, because programming is like a BS detector. My bad, I was trying to make my example simple to get across what I was trying to do & never even considered that the answer could be different if there were decimals involved. Hopefully I'll be a little more thorough next time, but I really appreciate the help from you guys here. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598252 Share on other sites More sharing options...
kicken Posted July 15, 2022 Share Posted July 15, 2022 9 hours ago, garyed said: never even considered that the answer could be different if there were decimals involved It's not just the decimals that you didn't consider / include in the problem description. If decimals were the only thing, then Barand's solution would have still worked fine as it was ok with decimals. You also should have considered / mentioned this part from the beginning: Quote I just need to find one specific unknown number in a specific string out of a huge string with multiple numbers. So you were not just looking for a number in a string, you were trying to find a specific number in a string that possibly contains multiple numbers. It's good to try and simplify the problem in general, you just need to be careful not to simplify it too much. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598257 Share on other sites More sharing options...
garyed Posted July 15, 2022 Author Share Posted July 15, 2022 2 hours ago, kicken said: It's not just the decimals that you didn't consider / include in the problem description. If decimals were the only thing, then Barand's solution would have still worked fine as it was ok with decimals. You also should have considered / mentioned this part from the beginning: So you were not just looking for a number in a string, you were trying to find a specific number in a string that possibly contains multiple numbers. It's good to try and simplify the problem in general, you just need to be careful not to simplify it too much. I see what you're saying now. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598267 Share on other sites More sharing options...
garyed Posted July 23, 2022 Author Share Posted July 23, 2022 On 7/14/2022 at 9:47 PM, kicken said: What you should do is spend a little time learning regular expressions, and play with them to find a working pattern. The pattern \d matches only digits (ie, 0 - 9). As such, it'll only match numbers the consist of simply digits (no decimals, no grouping characters). If you want to match decimals, you'd need to add . to the list of allowed characters. You can do that by specifying a set of possible characters using [ ]. Joe has ([\d.]+) large apples If you needed groups, you could likewise add , to the set. I have been learning & experimenting with regular expressions as you suggested & was just curious as to why you use the square brackets inside the curly brackets. I've found that whether I use the square brackets or not, the result is the same. Is it optional or is there a specific reason why they should be used? Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598512 Share on other sites More sharing options...
mac_gyver Posted July 23, 2022 Share Posted July 23, 2022 there are multiple ways of accoupling a task. you would need to post what you tried, along with the input data that 'worked'. Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598513 Share on other sites More sharing options...
garyed Posted July 23, 2022 Author Share Posted July 23, 2022 7 hours ago, mac_gyver said: there are multiple ways of accoupling a task. you would need to post what you tried, along with the input data that 'worked'. If I use either of these statements, the result is the same. Joe has ([\d.]+) large apples Joe has (\d.+) large apples It doesn't seem to matter whether I put the square brackets in or not. Are those brackets are there for a reason beyond the scope of the usage in this particular statement? Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598524 Share on other sites More sharing options...
mac_gyver Posted July 23, 2022 Share Posted July 23, 2022 (edited) there actually is a difference. try a number like .45 it won't match the (\d.+) case. this is because there isn't a numerical digit as the first character. the [] define a character set/class, that will match any of the characters in any order, even just a . character. without the [], there still can be any number of each character, but the first one must be a numerical digit. Edited July 23, 2022 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598525 Share on other sites More sharing options...
garyed Posted July 23, 2022 Author Share Posted July 23, 2022 Thank you for the explanation Quote Link to comment https://forums.phpfreaks.com/topic/315043-search-for-unknown-number-in-a-string/#findComment-1598543 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.