daydreamer Posted September 25, 2008 Share Posted September 25, 2008 I am trying to get a number from a string that will be in the range of 0.00 to 99.99. It will occur in the string like this: <p><em class="example"> 3.30 </em></p> spaces included! here is what i have so far: <?php preg_match("/(?=\"example\"> \s)[0-9]\.[0-9][0-9]+/",$xxx,$matches); print_r($matches); ?> What do i need to change? thanks. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 25, 2008 Share Posted September 25, 2008 <pre> <?php $data = <<<DATA <p><em class="example"> 3.30 </em></p> DATA; preg_match('/<em class="example">\s*(\d{1,2}.\d{2})/', $data, $matches); array_shift($matches); print_r($matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
daydreamer Posted September 25, 2008 Author Share Posted September 25, 2008 thanks alot! it works exactly how i need it to. been trying for ages. anyway, how come 3.30 gets saved into the array but "<em class="example">" does not? is it because that part of the expression is inside brackets? cheers! Quote Link to comment Share on other sites More sharing options...
effigy Posted September 25, 2008 Share Posted September 25, 2008 The whole pattern is stored in index 0, and the parenthetical content is stored in subsequent numbers. array_shift($matches); removed the entire pattern match. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted September 28, 2008 Share Posted September 28, 2008 Perhaps I'm splitting hairs here (I get the sense I am).. but I noticed the dot is not escaped in the pattern (thus it will capture any single character between the set of digits). depending on the data found in <em class="example">, the current pattern could find '3-30' for example. If there is a possibility to have something other than the dot between digits, would you not need to escape it in the pattern? preg_match('/<em class="example">\s*(\d{1,2}\.\d{2})/', $data, $matches); But again, depending on how the data that is found in <em class="example"> is decided / filtered, it probably doesn't matter? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 28, 2008 Share Posted September 28, 2008 Oh, good catch. Quote Link to comment 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.