edwardtilbury Posted November 13, 2008 Share Posted November 13, 2008 Here's what I have... A variable containing the following. "This complete set include the following parts: (12345), (12346), (12347)" How can I search this variable to pull out all the parts enclosed in parenthesis into an array? Thanks!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/ Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 explode ?? http://uk.php.net/explode <?php $var = "This complete set include the following parts: (12345), (12346), (12347)"; $var = explode("(",$var); $var = str_replace(")","",$var); print_r($var); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689727 Share on other sites More sharing options...
flyhoney Posted November 13, 2008 Share Posted November 13, 2008 Might be easier to do: <?php $numbers = array(); $subject = "This complete set include the following parts: (12345), (12346), (12347)"; $pattern = '/[\d]+/'; preg_match_all($pattern, $subject, $numbers); print_r($numbers); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689734 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 Can i ask how that is easier? Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689737 Share on other sites More sharing options...
edwardtilbury Posted November 13, 2008 Author Share Posted November 13, 2008 !@#$@, I wasn't expecting an answer that fast, let alone three answers! Amazing, I will try both of these out! Thank you!!! PS, the more answers to solve a problem the merrier! Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689740 Share on other sites More sharing options...
flyhoney Posted November 13, 2008 Share Posted November 13, 2008 Well, maybe not easier but possibly more fireproof. If the structure of the string changes, the regular expression should still be able to pick out the numbers. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689741 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 So should explode. Unless it changes too much Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689744 Share on other sites More sharing options...
flyhoney Posted November 13, 2008 Share Posted November 13, 2008 Here is the output of your solution: Array ( [0] => This complete set include the following parts: [1] => 12345, [2] => 12346, [3] => 12347 ) <3 <3 <3 Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689746 Share on other sites More sharing options...
Mark Baker Posted November 13, 2008 Share Posted November 13, 2008 So should explode. Unless it changes too much Although explode isn't picking out the numbers, it's picking out the content between the brackets. The regexp is explicitly picking out numbers wherever they appear in the haystack. Both should achieve the same result given this haystack (although you might want to unshift the first element from the result array for the explode method). It'd be interesting to see which was the faster of the two approaches Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689749 Share on other sites More sharing options...
Barand Posted November 13, 2008 Share Posted November 13, 2008 <?php $var = "This complete set include the following parts: (12345), (12346), (12347)"; $k = strlen($var); $p = $i = 0; $parts = array(); $inside = 0; while ($p < $k) { switch ($ch = $var[$p]) { case '(': $inside = 1; break; case ')': $inside = 0; ++$i; break; default: if ($inside) $parts[$i] .= $ch; } ++$p; } echo '<pre>', print_r($parts, true), '</pre>'; ?> --> Array ( [0] => 12345 [1] => 12346 [2] => 12347 ) Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689751 Share on other sites More sharing options...
flyhoney Posted November 13, 2008 Share Posted November 13, 2008 Regular expression are slow slow slow. But I think it is irrelevant in this case. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689752 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 Here is the output of your solution: Yes but he won't be using print_r, yours will get any numbers ? am i right, even ones not inside of parenthesis? Array ( [0] => This complete set include the following parts: [1] => 12345, [2] => 12346, [3] => 12347 ) To solve it he can do <?php $var = "This complete set include the following parts: (12345), (12346), (12347)"; $var = explode("(",$var); $arr = array(")",","); $var = str_replace($arr,"",$var); for($i = 1;$i < count($var);$i++) { echo $var[$i]."<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689761 Share on other sites More sharing options...
Mark Baker Posted November 13, 2008 Share Posted November 13, 2008 Regular expression are slow slow slow.Often said, less often true. Testing the following two variants on a function last week: function coordinateFromString($pCoordinateString = 'A1') { $column = rtrim($pCoordinateString, '0..9'); $row = ltrim($pCoordinateString, 'A..Z'); return array($column,$row); } function coordinateFromString($pCoordinateString = 'A1') { $column = ''; $row = ''; if (preg_match("/([$]?[A-Z]+)([$]?\d+)/", $pCoordinateString, $matches)) { list(, $column, $row) = $matches; } return array($column, $row); } The preg_match variant not only returned correct values in all cases (with inputs in the formats "IV123", "$IV123", "IV$123", "$IV$123") but also performed faster as well. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689764 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 I am obviously the winner here. <?php $var = "This complete set include the following parts: (12345), (12346), (12347)"; $n = 1; $p = false; for ($x = 0; $x < strlen($var); $x++) { if (is_numeric(substr($var,$x,1))) { $nums[$n] .= substr($var,$x,1); $p = true; } else { $p = false; $n++; } } $nums = array_values($nums); print_r($nums); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689775 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 Let's let edwardtilbury decide who is the winner So who's code are you going to use? Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689779 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 It's no contest. I won, end of story. I mean, I have sh!t in there I don't even know what it's there for. You just can't beat that! Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689795 Share on other sites More sharing options...
flyhoney Posted November 13, 2008 Share Posted November 13, 2008 Yeah I spent a good 5 minutes trying to figure out what all the $p = boolean business was doing. It's a very nice solution though. Cleaned up a lil: <?php $var = "This complete set include the following parts: (12345), (12346), (12347)"; for ($x = 0; $x < strlen($var); $x++) { if (is_numeric($var[$x])) { $nums[$n] .= $var[$x]; } else { $n++; } } $nums = array_values($nums); print_r($nums); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689802 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 haha it was supposed to be used to decide whether to inc $n or not but I decided who cares just array_values it and then forgot to take it out. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689806 Share on other sites More sharing options...
edwardtilbury Posted November 13, 2008 Author Share Posted November 13, 2008 I tried the solutions, but then realized that I present the wrong haystack! ::)DOH! So I cannot pick a winner yet. You guys are too helpful! Ok, I made a mistake, I checked the database, and actually it's a little more complicated... ??? The variable has numbers and text, also the needles are not separated uniformly. Please solve for this new haystack... Haystack 2.0 This 19th century easter egg basket includes the following parts: A giant robot from the year 2008 (12345), Left window trim for a 1993-2001 Lexus SC400 (12346), and a 1/6th scale papier-mache head of Ron Paul made from native indians in Luxembourg(12347) Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689809 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 if you are just wanting to get the numbers in parenthesis's then my solution should work. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689811 Share on other sites More sharing options...
.josh Posted November 13, 2008 Share Posted November 13, 2008 @flyhoney: Anyways, dunno about it being a "very nice" solution... I personally would opt for the regex route. I was just being silly. And on that note... I tried the solutions, but then realized that I present the wrong haystack! ::)DOH! So I cannot pick a winner yet. You guys are too helpful! Ok, I made a mistake, I checked the database, and actually it's a little more complicated... ??? The variable has numbers and text, also the needles are not separated uniformly. Haystack 2.0 This 19th century easter egg basket includes the following parts: A giant robot from the year 2008 (12345), Left window trim for a 1993-2001 Lexus SC400 (12346), and a 1/6th scale papier-mache head of Ron Paul made from native indians in Luxembourg Use the regex solution just alter it to only grab numbers between parenthesis. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689812 Share on other sites More sharing options...
flyhoney Posted November 13, 2008 Share Posted November 13, 2008 Like this: <?php $numbers = array(); $subject = 'This 19th century easter egg basket includes the following parts: A giant robot from the year 2008 (12345), Left window trim for a 1993-2001 Lexus SC400 (12346), and a 1/6th scale papier-mache head of Ron Paul made from native indians in Luxembourg'; $pattern = '/\(([\d]+)\)/'; preg_match_all($pattern, $subject, $numbers); $numbers = array_pop($numbers); print_r($numbers); ?> I was being facetious . Either way, it is a creative solution. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689815 Share on other sites More sharing options...
Barand Posted November 13, 2008 Share Posted November 13, 2008 Mine still works Array ( [0] => 12345 [1] => 12346 ) Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689817 Share on other sites More sharing options...
DeanWhitehouse Posted November 13, 2008 Share Posted November 13, 2008 No.....! mine is simplest, he can edit it easily and doesn't need to know regex Or not, lol, just tested it. Regex is the best way unless you want a even more complex code. Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689818 Share on other sites More sharing options...
flyhoney Posted November 13, 2008 Share Posted November 13, 2008 Also, wtf? and a 1/6th scale papier-mache head of Ron Paul made from native indians in Luxembourg Quote Link to comment https://forums.phpfreaks.com/topic/132635-needles-in-haystack-no-idea-how-to-parse-break-apart/#findComment-689819 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.