0

Hi,

I have the following problem. How do i get the number out of this variable:

<div class="right"><a id="ctl111_contestProfileLink" href="http://contests.covers.com/Sportscontests/profile.aspx?user=265839&amp;sportid=0">Picks History</a></div>

The numbers can range from -1000000 till +10000000. Does anyone know how can I achieve that? I saw someone do it with eregi() function, but since this will be deprecated in PHP 6, I wonder if you could do it with preg_match and how?

Or maybe via an other way?

Edited by Kligham: n/a

5
Contributors
9
Replies
11
Views
7 Years
Discussion Span
Last Post by hielo
0

I assume that you're not using asp! Are you trying to get the id number or the href user number?

I'm a little confused as to what you're trying to achieve.

How is this DIV shown? Is it hard coded (vanilla html) or is it created in a php string?

Are you trying to pick up the number when the link is clicked, i.e. via $_GET? If so this won't work with an aspx file.

Sorry if I'm being a bit thick.

0

Don't claim to be an expert on regular expressions, but I'll try to explain what the code does. preg_match will search a string and try to match the given perl-compatible pattern.

#user= will first try to find the word "user=". Next, it'll search for a digit (0-9), as represented by \d. The plus sign following it means to search for 1 or more of the previously noted character, which is a digit. This part is contained within parenthesis to specify a group. Once it reaches a non-digit, it terminates that particular match search and starts a new match to the pattern. Any text contained within the string which matches the pattern user=34895430 (or any number of digits following user=) will have the number added to the $m array, as it was the "grouped" part.

Here's a handy regular expression cheat sheet.

Edited by Phaelax: n/a

0

Don't claim to be an expert on regular expressions, but I'll try to explain what the code does. preg_match will search a string and try to match the given perl-compatible pattern.

#user= will first try to find the word "user=". Next, it'll search for a digit (0-9), as represented by \d. The plus sign following it means to search for 1 or more of the previously noted character, which is a digit. This part is contained within parenthesis to specify a group. Once it reaches a non-digit, it terminates that particular match search and starts a new match to the pattern. Any text contained within the string which matches the pattern user=34895430 (or any number of digits following user=) will have the number added to the $m array, as it was the "grouped" part.

Here's a handy regular expression cheat sheet.

Ok, I think I get what you want to explain. However in the example above it also possible that number=-25326. How do I do that? I tried the following things but they didn't work:

preg_match('#user=([:punct:]+\d+)#',$str,$m);

But this one give me a: Compilation failed: POSIX named classes are supported only within a class at offset 6

preg_match('#user=(-\d+)#',$str,$m);

This also doesn't work.

On final question: I wanted to search for 500</td><td> but that also didn't work. I tried:

preg_match('#500/&lt;td&gt;&lt;td&gt;(\w+)#',$str,$m);

and

preg_match('#500</td><td>(\w+)#',$str,$m);

This was to get Won out of this variable:

$test = "<td>102</td><td>Over 196.5</td><td>500</td><td>WON</td><td>+500</td>";

It must be stated that the WON can also be PUSH or LOSS.

I hope someone can help me out with these question =)

Edited by Kligham: n/a

0

try this.

<?php

//for - and + values

$str = '<div class="right"><a id="ctl111_contestProfileLink" href="http://contests.covers.com/Sportscontests/profile.aspx?user=265839&amp;sportid=0">Picks History</a></div>';

preg_match('/user=([-0-9]+)/',$str,$m);
echo "User is: " . $m[1];
?>


<?php

//html value search

$html = "<td>102</td><td>Over 196.5</td><td>500</td><td>WON</td><td>+500</td>";

preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);

foreach ($matches as $val) {
    echo "matched: " . $val[0] . "<br>";
}
?>

Edited by phpbeginners: n/a

1
preg_match('/user=([-0-9]+)/',$str,$m);

That won't work as desired. It will allow entries such as:
90-345

I believe the poster intends that hyphen to be "OPTIONAL and only at the beginning of the numeric substring, in which case he needs:

preg_match('/user=([-]?\d+)/',$str,$m);

where [-]? indicates an optional hyphen, and \d+ indicates one or more digits

It must be stated that the WON can also be PUSH or LOSS.

in that case search for any of those possible three values:

$str = "<td>102</td><td>Over 196.5</td><td>500</td><td>WON</td><td>+500</td>";

preg_match('\b(WON|PUSH|LOSS)\b',$str,$m);
echo $m[1];//this will echo the actual match
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.