Jump to content

preg_match_all problem with pattern


JohnyBazooka

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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%.

Link to comment
Share on other sites

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%?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.