JohnyBazooka Posted March 27, 2009 Share Posted March 27, 2009 Hello there, as you can see i am totaly newbie here, so at first - sorry for my bad english.. And sorry that my first post is asking for help, but i am really at deadend. I am was googling more than 4hours my problem, but i experienced that i wont find solution for my problem and i have to study regex I have bookmarks some webpages with regex (http://www.regular-expressions.info/ for example), but i need make one pattern for my web. Please, if someone can help with this > i have this source code: <td style="padding: 10px; text-align: center; font-weight: bold; font-size: 36px; color: white; background-color: rgb(117, 132, 171);"> 61% </td> and i want from there that number with % (it can be other number ofcourse)... Please can you help me with that pattern? Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/ Share on other sites More sharing options...
Jagarm Posted March 27, 2009 Share Posted March 27, 2009 Try the following: <[^>]*>([^\r]*?)</.*> the result that you get from there may contain additional spaces which you must trim it. Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-795179 Share on other sites More sharing options...
JohnyBazooka Posted March 27, 2009 Author Share Posted March 27, 2009 Thanks for quick reply, but didnt help that code... Results > more than i want... Is possible to have that style=.... in that pattern? It is only way to match only that one result that i want. Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-795394 Share on other sites More sharing options...
nrg_alpha Posted March 27, 2009 Share Posted March 27, 2009 You want the numbers then the %? Example: $str = <<<DATA <td style="padding: 10px; text-align: center; font-weight: bold; font-size: 36px; color: white; background-color: rgb(117, 132, 171);"> 61% </td> DATA; preg_match('#<td.*?style=[^>]+>.*?(\d+%).*?</td>#s', $str, $match); echo $match[1]; // outputs 61% Note, after the td, I put .*? in case there might be some stuff between that, like <td class="whatever" style=.....> In either case, it should work. Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-795447 Share on other sites More sharing options...
JohnyBazooka Posted March 28, 2009 Author Share Posted March 28, 2009 That work too, but i have more results that i want too... Can you please make pattern with exact formula in it > <td style="padding: 10px; text-align: center; font-weight: bold; font-size: 36px; color: white; background-color: rgb(117, 132, 171);"> 61% </td> I am really happy, that you want to help me Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-795956 Share on other sites More sharing options...
nrg_alpha Posted March 28, 2009 Share Posted March 28, 2009 I don't think you understand (unless I am missing something here). The (\d+%) part in the pattern captures a digit one or more times, followed by the % sign, and stores that into the variable $match[1]. Are you using $match[1]? Can you provide a small sample of code where you are using this, as well as what you are checking? Because when you run my small sample, it will only give 61%. Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-795957 Share on other sites More sharing options...
JohnyBazooka Posted March 29, 2009 Author Share Posted March 29, 2009 Look at result of that array > Array ( [0] => 100% [1] => 100% [2] => 100% [3] => 75% [4] => 61% [5] => 100% [6] => 100% ) Array So there is more numbers with %.... Look at this page > here is example from what i need to download that text... http://www.csfd.cz/film/221839-hluboko-v-gangu-waist-deep/ You will need this > function curl_file_get_contents($url) { $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_URL, $url); $contents = curl_exec($c); curl_close($c); return $contents; } $data = curl_file_get_contents("http://www.csfd.cz/film/221839-hluboko-v-gangu-waist-deep/"); Is possible to match exactly that one number 61%? Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-796216 Share on other sites More sharing options...
nrg_alpha Posted March 29, 2009 Share Posted March 29, 2009 Well, you could change the preg line to read: if(preg_match('#<td.*?style=[^>]+>.*?(61%).*?</td>#s', $data)){ // do something } Is that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-796294 Share on other sites More sharing options...
JohnyBazooka Posted March 29, 2009 Author Share Posted March 29, 2009 Sorry, but no :-\ That number is not every time 61, but can vary... This one number is between that td with that style... http://www.csfd.cz/film/1644-kmotr-godfather-the/ http://www.csfd.cz/film/10135-forrest-gump/ look at that > numbers with % - that is rating... i want to use that ratings on my web... i can that manually type, but i think this is time for preg_match_all Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-796415 Share on other sites More sharing options...
nrg_alpha Posted March 29, 2009 Share Posted March 29, 2009 Then my suggestion is to use my first preg example, and search through your array using foreach and track down which percent you want. Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-796432 Share on other sites More sharing options...
JohnyBazooka Posted March 29, 2009 Author Share Posted March 29, 2009 100% [1] => 100% [2] => 100% [3] => 75% [4] => 61% [5] => 100% [6] => 100% - i dont know which is the right one, when i see this... There is right number that 61, but only because i know that it is 61... Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-796440 Share on other sites More sharing options...
nrg_alpha Posted March 29, 2009 Share Posted March 29, 2009 For example: $arr = array('100%', '100%', '100%', '75%', '61%', '100%', '100%'); // an example array foreach ($arr as $val) { if($val == '61%'){ // do something } } So instead of $arr, use the array name you are using instead. I would suggest doing some php tutorials on arrays, and different loops, like for, foreach, etc.. (just google it, there is plenty of material floating around to help you out). Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-796458 Share on other sites More sharing options...
sasa Posted March 30, 2009 Share Posted March 30, 2009 preg_match('/<td style=[^>]+>\s*(\d+%)\s*</', $str, $match); echo $match[1]; Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-796771 Share on other sites More sharing options...
JohnyBazooka Posted March 30, 2009 Author Share Posted March 30, 2009 preg_match('/<td style=[^>]+>\s*(\d+%)\s*</', $str, $match); echo $match[1]; Thank you very much sasa. This is exactly what i need. I test it for few pages and it seems to work perfectly Btw. thanks everyone who try to help! Quote Link to comment https://forums.phpfreaks.com/topic/151343-preg_match_all-problem-with-pattern/#findComment-797129 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.