473,378 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Find dynamic number before static string

384 256MB
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.  

7 1825
ziycon
384 256MB
Or is it possible to return a just the string that contains say '\50'?
May 16 '10 #2
Atli
5,058 Expert 4TB
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
ziycon
384 256MB
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
5,058 Expert 4TB
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
ziycon
384 256MB
Thanks for that, I'll give it a go later on when I get a chance. Much appreciated.
May 17 '10 #6
ziycon
384 256MB
Worked perfectly, thanks for your help. Any good sites you'd recommend for reading up on reg. expressions?
May 17 '10 #7
Atli
5,058 Expert 4TB
I'd check out regular-expressions.info. It has a nice tutorial, and a lot of other info.
May 17 '10 #8

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

Similar topics

2
by: rmathieu | last post by:
Hi, I want to initialize a static String array in MC++. What I want to do is to initialize my String array like the C# way: new String {"11", "22"} but I could not find an equivalent in MC++. The...
4
by: Mike Moore | last post by:
We are developing an asp.net web application. When we drag and drop the sql connection object to the form we set the in the dynamic properties connection string to use the connection string in the...
7
by: Chris Thunell | last post by:
I'm looking to find in a long string an instance of 4 numbers in a row, and pull out those numbers. For instance: string = "0104 PBR", i'd like to get the 0104. string="PBR XT 0105 TD", i'd like...
9
by: atbusbook | last post by:
I'm doing a report on the speed of develipment and executionin varius programing langiuiges. write code for all these tasks in the languige of your choise if intrestied send code to...
3
by: Bosconian | last post by:
I'm looking for a way to pass a dynamic number of args to sprintf. So instead of $a = "a"; $b = "b"; $c = "c"; sprintf($format, $a, $b, $c) one could
4
by: BA | last post by:
Hello, I have a very strange code behavior that I cannot make heads or tails of: I have c# code being executed in BizTalk assemblies which is acting very strangely. In my BizTalk process I...
0
by: DennieOMP | last post by:
Hello! I have a question to OpenMP. In loop parallelisation, there is the option schedule and in this, one can use static and dynamic. What is the exact difference between them? Here an example...
7
by: BillG | last post by:
Hi, Does anyone know of a site or have code for a function that will generate a random string or random number? I need one where I can tell it what type of value I need and where I can set the...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
2
by: Austin326 | last post by:
Hi Everyone, I have a problem that is quite frusturating. I am passing in an image from a database, which is to be accessed in an image button. When I dynamically add the string for an sql...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.