By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,299 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,299 IT Pros & Developers. It's quick & easy.

Find dynamic number before static string

100+
P: 384
I need to get the number before a static string for example the string would be something like 12/50, the /50 will always be the same but I need to know how to find out the dynamic number before the /50 as it will always be changing??
May 16 '10 #1

✓ answered by Atli

Ahh ok. For that you could use a regular expression.

It could be as simple as:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Fetch the contents of the page
  3. $pageContents = file_get_contents("http://example.com");
  4.  
  5. // Create the regexp pattern
  6. $regexp = '#(\d+)/(\d+)#';
  7.  
  8. // Find all matches for the regexp
  9. // in the page contents.
  10. if(preg_match_all($regexp, $pageContents, $matches)) 
  11. {
  12.     // Print the array of matches.
  13.     echo "<pre>", print_r($matches, true), "</pre>";
  14. }
  15. else
  16. {
  17.     echo "Nothing found!";
  18. }
  19. ?>
To explain how the regular expression pattern works, this is how I look at it:
  1. I start with two numbers, which are represented by a the \d characters.
    Expand|Select|Wrap|Line Numbers
    1. \d\d
  2. And they should be allowed to be more than one number (a string of numeric characters), so I add the + sign to the character class. The + sign means: "One or more of the preceding character".
    Expand|Select|Wrap|Line Numbers
    1. \d+\d+
  3. The I separate them by a forward slash, like in the string you posted.
    Expand|Select|Wrap|Line Numbers
    1. \d+/\d+
  4. To allow PHP to fetch each number separately, I enclose them both in parenthesis. They create what is referred to as a "group", which can be used to reference the match both inside and outside the expression. (In our case, we are only concerned about the "outside" part, which allows PHP to read them into an array for us.)
    Expand|Select|Wrap|Line Numbers
    1. (\d+)/(\d+)
  5. The whole thing is then enclosed in custom delimiters, as per PHP's PCRE implementation. I chose the # chars, but it can be any char you want.
    Expand|Select|Wrap|Line Numbers
    1. #(\d+)/(\d+)#
And that's it. When executed as in the code above, PHP might print something like:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [0] => 20/50
  6.             [1] => 25/40
  7.             [2] => 100/545
  8.         )
  9.  
  10.     [1] => Array
  11.         (
  12.             [0] => 20
  13.             [1] => 25
  14.             [2] => 100
  15.         )
  16.  
  17.     [2] => Array
  18.         (
  19.             [0] => 50
  20.             [1] => 40
  21.             [2] => 545
  22.         )
  23.  
  24. )
  25.  

Share this Question
Share on Google+
7 Replies


100+
P: 384
Or is it possible to return a just the string that contains say '\50'?
May 16 '10 #2

Atli
Expert 5K+
P: 5,058
Hey.

Have you tried the explode function?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // This can come from anywhere.
  3. $string = "12/50";
  4.  
  5. // "Explode" the string on the / char.
  6. $parts = explode("/", $string);
  7.  
  8. // Then access the parts from the result array.
  9. echo $parts[0]; // 12
  10. echo $parts[1]; // 50
  11. ?>
May 16 '10 #3

100+
P: 384
The problem is that I'm grabbing a webpage and searching for a certain string like 12/50 and I need to get just the number before the forward slash which is dynamic. So I can't use explode as its not just 12/50, there is everything else thats on the page, if you get me.

Expand|Select|Wrap|Line Numbers
  1. $original_file = file_get_contents($page);
May 16 '10 #4

Atli
Expert 5K+
P: 5,058
Ahh ok. For that you could use a regular expression.

It could be as simple as:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Fetch the contents of the page
  3. $pageContents = file_get_contents("http://example.com");
  4.  
  5. // Create the regexp pattern
  6. $regexp = '#(\d+)/(\d+)#';
  7.  
  8. // Find all matches for the regexp
  9. // in the page contents.
  10. if(preg_match_all($regexp, $pageContents, $matches)) 
  11. {
  12.     // Print the array of matches.
  13.     echo "<pre>", print_r($matches, true), "</pre>";
  14. }
  15. else
  16. {
  17.     echo "Nothing found!";
  18. }
  19. ?>
To explain how the regular expression pattern works, this is how I look at it:
  1. I start with two numbers, which are represented by a the \d characters.
    Expand|Select|Wrap|Line Numbers
    1. \d\d
  2. And they should be allowed to be more than one number (a string of numeric characters), so I add the + sign to the character class. The + sign means: "One or more of the preceding character".
    Expand|Select|Wrap|Line Numbers
    1. \d+\d+
  3. The I separate them by a forward slash, like in the string you posted.
    Expand|Select|Wrap|Line Numbers
    1. \d+/\d+
  4. To allow PHP to fetch each number separately, I enclose them both in parenthesis. They create what is referred to as a "group", which can be used to reference the match both inside and outside the expression. (In our case, we are only concerned about the "outside" part, which allows PHP to read them into an array for us.)
    Expand|Select|Wrap|Line Numbers
    1. (\d+)/(\d+)
  5. The whole thing is then enclosed in custom delimiters, as per PHP's PCRE implementation. I chose the # chars, but it can be any char you want.
    Expand|Select|Wrap|Line Numbers
    1. #(\d+)/(\d+)#
And that's it. When executed as in the code above, PHP might print something like:
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [0] => 20/50
  6.             [1] => 25/40
  7.             [2] => 100/545
  8.         )
  9.  
  10.     [1] => Array
  11.         (
  12.             [0] => 20
  13.             [1] => 25
  14.             [2] => 100
  15.         )
  16.  
  17.     [2] => Array
  18.         (
  19.             [0] => 50
  20.             [1] => 40
  21.             [2] => 545
  22.         )
  23.  
  24. )
  25.  
May 17 '10 #5

100+
P: 384
Thanks for that, I'll give it a go later on when I get a chance. Much appreciated.
May 17 '10 #6

100+
P: 384
Worked perfectly, thanks for your help. Any good sites you'd recommend for reading up on reg. expressions?
May 17 '10 #7

Atli
Expert 5K+
P: 5,058
I'd check out regular-expressions.info. It has a nice tutorial, and a lot of other info.
May 17 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.