0

I would like to extract labels and data from a string using php. The string looks like this:

 Label: Info;
 Label1: Info1;
 Label2: Info2;
 Label3: Info3;
 ..............

My solution is to use strpos for every label , retain it in a variable and extract data for that label which is too slow. Could you suggest me another method ?

3
  • 1
    Where is your code? We can't say why it's too slow. Commented Jul 12, 2012 at 14:28
  • Do you need to extract all the labels and their data or are you only looking for some specific labels? Commented Jul 12, 2012 at 14:29
  • @SilverSnake for some specific labels , to extract data. Commented Jul 12, 2012 at 14:31

1 Answer 1

2

I am most certainly not an expert with REGEX, but this is the solution I came up with that worked.

$ptn = "/(Label[0-9]?):?.?(.+);/";
$str = "Label: Info;
Label1: Info1;
Label2: Info2;
Label3: Info3;";
preg_match_all($ptn, $str, $matches, PREG_SET_ORDER);

Now using print_r on $matches returns the following:

Array
(
    [0] => Array
        (
            [0] => Label: Info;
            [1] => Label
            [2] => Info
        )

    [1] => Array
        (
            [0] => Label1: Info1;
            [1] => Label1
            [2] => Info1
        )

    [2] => Array
        (
            [0] => Label2: Info2;
            [1] => Label2
            [2] => Info2
        )

    [3] => Array
        (
            [0] => Label3: Info3;
            [1] => Label3
            [2] => Info3
        )

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

Comments

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.