1

I have been working on a script that pulls information from a certain website. The said website pulls the information from a database and displays it in a way the user can easily read it (like always).

Imagine it looks like this:

Var1: result1 Var2: result2 Var3: result3

What my script does is that it reads the page's source code and retrieves "result1", "result2" and "result3" by obtaining the text between two strings.

Sample code:

<?php

    function get_string_between($string, $start, $end) {

        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);

    }

    function check($url) {

        // usually, $fullstring = file_get_contents($url);
        $fullstring = "<string1>result1</string1><string1>result2</string1><string1>result3</string1>";

        $result = get_string_between($fullstring, "<string1>", "</string1>");

        echo "<b>Result: </b>".$result;

    }

    check("random");    // just to execute the function

?>

In case you wonder why I have the check() function there it is because this code is part of something bigger and I need a solution that works in this case scenario, so I tried to keep it immaculate.

Now, I can easily get "result1" because it's the first occurrence, but how can I get "result2" and "result3"?

Thank you :)

4
  • Does this article help? Commented Aug 7, 2012 at 18:40
  • I had to re-read the code a few times, but I understand what you're trying to do here. Commented Aug 7, 2012 at 18:40
  • @Matt I found the sample very helpful, it represents another way to do this and it is definitely a relevant example for this situation. The problem is that I don't know exactly how many occurrences of the string I will be dealing with and what the strings will look like(I should have stated this in the original question). In case I am misunderstanding the code, doesn't it retrieve the position of a certain string(that you define)? Commented Aug 7, 2012 at 18:49
  • Check out @nickb's answer. That will get you what you need. If you return the entire array of matches that his code gives you, you can then iterate through it using a foreach. Commented Aug 7, 2012 at 18:52

1 Answer 1

3

Use a regex to extract all of the matches, then pick the ones you want:

function get_string_between($string, $start, $end) 
{
    preg_match_all( '/' . preg_quote( $start, '/') . '(.*?)' . preg_quote( $end, '/') . '/', $string, $matches);
    return $matches[1];
}

The regex will capture anything between the $start and $end variables.

Now the function returns an array of all of the result values, which you can pick which one you want:

list( $first, $second, $third) = get_string_between( $string,  "<string1>", "</string1>");

You can see it working in this demo.

Sign up to request clarification or add additional context in comments.

6 Comments

Take it a step further and you can add $matches as a reference parameter function get_string_between($string, $start, $end, &$matches) {...}
Thanks for your answer, I have two questions. 1) What can I do if I don't know the number of occurrences (i can have 1,2,3,4,5..200 times <string1>text</string1>) and 2) How can I retrieve ONLY "result1" instead of "string 'result1' (length=7)"? Thanks again
Or you can return $matches and the programmer can then process it as an array instead of just a string.
This returns an array - It'll work with any number of occurrences. Your second question is trivial and shows a lack of understanding of the language - The output you're seeing is from var_dump() to show you the type of the variable. If you just did echo $first; on my demo, you wouldn't get var_dump()'s output.
@nickb Yes, I do lack knowledge when it comes to php and I realise it shows, that is probably the reason why I will be asking the following question, one that looks basic to you: How can I make it work with a random number of occurrences? In your example you set $list( $first, $second, $third), implying we only have three occurrences.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.