0

I am trying to get all values from a large string output where input name = "something";

My curl function returns response in a String format (in fact its a complete html page). Most of the info is useless however inside this string i have information that i would like to get

<input name="queueID" value="4795" type="checkBox" checked>aaaa
<br>

<input name="queueID" value="4799" type="checkBox" checked>bbbb
<br>

<input name="queueID" value="4796" type="checkBox" checked>cccc
<br>

<input name="queueID" value="4794" type="checkBox" checked>dddd
<br>

NOTE: That the number of queueID is dynamic so i can have 2 queues or 10 queues. and also The number on each queue can be different, not necessary in order. the input name is ALWAYS queueID.

As of doing this with REGEX i truly have no idea how to do it, and if someone knows i would appreciate the answer with regex solution.

i have tried doing this in more of a static way such as

    $needle = array (' ' ' ' ' ' ' ' );
    $pattern  = '' . implode('|', array_map('preg_quote', $needle)) . '/i';

    foreach($file_list as $file) {
        if(preg_match($pattern, $file)) {

        }
    }

however this will only work if I know how many queues i have and what are the numbers for this queue. Could any one suggest a decent solution for this problem ?

3 Answers 3

1
preg_match_all('/(?<=^<input name="queueID" value=")\d+/mi', $str, $values);
Sign up to request clarification or add additional context in comments.

2 Comments

Workks like a charm :) . i need to get me teeth into regex at some point as its quite useful!.
It really is. Some people hate it (it can get very complex) but others, myself included, find it very satisfying. You either dig it or you don't. The good news is it translates well between different languages; the implementations are normally similar if not completely identical in terms of the syntax used.
1

What about on preg_match_all on the following PCRE regexp ?

preg_match_all('#<input.*?>(.*?)\n<br>#', $html, $matches);

Here's what I get

Comments

1

If you use regex, you'll face a problem when the ordering of the name-value is changed. For example this one, which is slightly different from your input:

value="4794" name="queueID"

You can try this lookahead regex((?=[^>]*name="queueID")) that first checks whether the <input tag contains the name="queueID" or not. After that it parses the Value.

$input = '<input name="queueID" value="123" type="checkBox" checked>aaaa
<input name="queueID" value="456" type="checkBox" checked>bbbb
<input name="xqueueID" value="789" type="checkBox" checked>bbbb
<input name="queueID" value="101112" type="checkBox" checked>cccc
<input value="131415" name="queueID" type="checkBox" checked>dddd';

preg_match_all('/<input\b(?=[^>]*name="queueID")[^>]*\bvalue="([^"]+)"/i', $input, $match);
print_r($match[1]);

Output:

[0] => 123
[1] => 456
[2] => 101112
[3] => 131415

1 Comment

Beware of greedy matches (+ sign), Sabuj ! Cheers

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.