soryn4u Posted August 20, 2009 Share Posted August 20, 2009 i have a problem for example i want to use eregi to retrive data i want to retrive data from an website the code from the page looks like this <li id="cote">23:55</li> <li id="cote">1.40</li> <li id="cote"> 4.00 </li> <li id="cote"> 1.04 </li> <li id="cote"> 2.49 </li> <li id="cote"> 1.15 </li> if i use if (eregi('<li id="cote">(.*)</li>', $line, $out)) { $categ = each ($out); echo $categ[1]."\n"; } the result is <li id="cote">23:55</li> <li id="cote">1.40</li> i retrive only a part of the code becouse are whitespaces or new lines between <li id="cote">(.*)</li> how to use eregi... to retrive all the data ? becouse works only this part <li id="cote">23:55</li> <li id="cote">1.40</li> and i lose this part <li id="cote"> 4.00 </li> <li id="cote"> 1.04 </li> <li id="cote"> 2.49 </li> <li id="cote"> 1.15 </li> Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/ Share on other sites More sharing options...
ignace Posted August 20, 2009 Share Posted August 20, 2009 $line is probably one line of the file. You can't use that here as some parts are on new lines. You can use eregi() to retrieve all the data from the file without having to use file(). $contents = file_get_contents('path/to/file'); $matches = array(); preg_match('/\d{1,2}(:|.)\d{1,2}/', $contents, $matches); print_r($matches); Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-902608 Share on other sites More sharing options...
soryn4u Posted August 20, 2009 Author Share Posted August 20, 2009 if i use youre code <?php $contents =' <li id="cote">23:55</li> <li id="cote">1.40</li> <li id="cote"> 4.00 </li> <li id="cote"> 1.04 </li> <li id="cote"> 2.49 </li> <li id="cote"> 1.15 </li> '; $matches = array(); preg_match('/\d{1,2}(:|.)\d{1,2}/', $contents, $matches); print_r($matches); //the result is ?> Array ( [0] => 23:55 [1] => : ) and i lose: 1.40 4.00 1.04 2.49 1.15 Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-902620 Share on other sites More sharing options...
ignace Posted August 20, 2009 Share Posted August 20, 2009 $matches = array(); $contents = file_get_contents('path/to/file'); preg_match_all('/\d{1,2}(:|\.)\d{1,2}/', $contents, $matches); print_r($matches); Sorry wrong function Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-902685 Share on other sites More sharing options...
soryn4u Posted August 20, 2009 Author Share Posted August 20, 2009 Thaks it`s working i want you to ask...if i want to retrieve some complex data like strings and numbers in same time its possible to do this thing? for example <?php $contents =' <li id="cote">8655</li> <li id="echipe"> Flamengo : Cruzeiro </li> <li id="cote">23:55</li> <li id="cote"> 2.00 </li> <li id="cote"> 3.25 </li> <li id="cote"> 3.25 </li> <li id="cote"> 1.24 </li> <li id="cote"> 1.63 </li> <li id="cote"> 1.24 </li> '; ?> and result to be: 8655 Flamengo : Cruzeiro 23:55 2.00 3.25 3.25 1.24 1.63 1.24 Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-902704 Share on other sites More sharing options...
ignace Posted August 21, 2009 Share Posted August 21, 2009 $matches = array(); $contents = file_get_contents('path/to/file'); preg_match_all('/\d{1,2}(:|\.)\d{1,2}/', $contents, $matches); print_r($matches); Sorry wrong function $matches = array(); $contents = file_get_contents('path/to/file'); preg_match_all('/([a-z0-9]+(:|\.)[a-z0-9]+)/i', $contents, $matches); print_r($matches); May work but will not match 8655 and Flamengo : Cruzeiro if they contain a space before and after :. Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-903393 Share on other sites More sharing options...
soryn4u Posted August 21, 2009 Author Share Posted August 21, 2009 thanks man for your time i add 2 extra code and now matches and 8655 and Flamengo : Cruzeiro amazing <?php $matches = array(); preg_match_all('/([a-z0-9]+(:|\.| : |[0-9])[a-z0-9]+)/i', $contents, $matches); print_r($matches); ?> Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-903452 Share on other sites More sharing options...
soryn4u Posted August 21, 2009 Author Share Posted August 21, 2009 Topic CLOSED Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-903453 Share on other sites More sharing options...
nrg_alpha Posted August 21, 2009 Share Posted August 21, 2009 If I understand correctly, would something like this work for you? $html = <<<EOF <li id="cote">8655</li> <li id="echipe"> Flamengo : Cruzeiro </li> <li id="cote">23:55</li> <li id="cote"> 2.00 </li> <li id="cote"> 3.25 </li> <li id="cote"> 3.25 </li> <li id="cote"> 1.24 </li> <li id="cote"> 1.63 </li> <li id="cote"> 1.24 </li> EOF; preg_match_all('#<li[^>]*>([^<]+)</li>#', $html, $matches); unset($matches[0]); $matches[1] = array_map('trim', $matches[1]); echo '<pre>'.print_r($matches[1], true); Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-903460 Share on other sites More sharing options...
nrg_alpha Posted August 21, 2009 Share Posted August 21, 2009 Topic CLOSED Actually, to close a topic, you flag it solved. Oh, and just for future reference, I wouldn't use ereg anymore.. as of the current stable PHP release (5.3), it is already depreciated... you should learn pcre instead, as posix (which ereg come from) will no longer be included within the core of PHP as of version 6. Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-903461 Share on other sites More sharing options...
soryn4u Posted August 22, 2009 Author Share Posted August 22, 2009 thanks for your solution...and for advices nrg_alpha Link to comment https://forums.phpfreaks.com/topic/171164-solved-how-to-use-eregi/#findComment-903864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.