1

Consider that i have the string,

$string = 'tag2 display="users" limit="5"';

Using the preg_match_all function, i need to get the output

Required o/p

Array
(
    [0] => Array
        (
            [0] => tag2
            [1] => tag2
            [2] => 
        )

    [1] => Array
        (
            [0] => display="users"
            [1] => display
            [2] => users
        )

    [2] => Array
        (
            [0] => limit="5"
            [1] => limit
            [2] => 5
        )

)

I tried using this pattern '/([^=\s]+)="([^"]+)"/' but it is not recognizing the parameter with no value (in this case tag2) Instead it gives the output

What I am getting

Array
(
    [0] => Array
        (
            [0] => display="users"
            [1] => display
            [2] => users
        )

    [1] => Array
        (
            [0] => limit="5"
            [1] => limit
            [2] => 5
        )

)

What will be the pattern for getting the required output ?

EDIT 1: I also need to get the attributes which are not wrapped with quotes ex: attr=val. Sorry for not mentioning before.

3 Answers 3

2

Try this:

<?php

    $string = 'tag2 display="users" limit="5"';
    preg_match_all('/([^=\s]+)(="([^"]+)")?/', $string, $res);

    foreach ($res[0] as $r => $v) {
        $o[] = array($res[0][$r], $res[1][$r], $res[3][$r]);
    }

    print_r($o);

?>

It outputs me:

Array
(
    [0] => Array
        (
            [0] => tag2
            [1] => tag2
            [2] => 
        )

    [1] => Array
        (
            [0] => display="users"
            [1] => display
            [2] => users
        )

    [2] => Array
        (
            [0] => limit="5"
            [1] => limit
            [2] => 5
        )

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

5 Comments

somewhat similar... it will be good if i got the required o/p, since my other functions depend on the exact required array..
I made some changes to fit the output you defined.
great answer thank you... but when i tried without quotes, its not working... i.e param=val sorry I should have told this before..
No problem, patch with this regexp: /([^=\s]+)(="?([^"]+)"?)?/
i tried hakre's pattern /([^=\s]+)(?:="([^"]+)")?/, which came out really well. But it is not working without quotes... what will be the fix... i am sure it will be easy..
1

I think it's not fully possible to give you with one call what you're looking for, but this is pretty close:

$string = 'tag2 display="users" limit=5';
preg_match_all('/([^=\s]+)(?:="?([^"]+)"?|())?/', $string, $res, PREG_SET_ORDER);
print_r($res);

Output:

Array
(
    [0] => Array
        (
            [0] => tag2
            [1] => tag2
            [2] => 
            [3] => 
        )

    [1] => Array
        (
            [0] => display="users"
            [1] => display
            [2] => users
        )

    [2] => Array
        (
            [0] => limit=5
            [1] => limit
            [2] => 5
        )

)

As you can see, the first element has no value, I tried to work around that and offer an empty match now. So this builds the array you were asking for, but has an additional entry on the empty attribute.

However the main point is the PREG_SET_ORDER flag of preg_match_all. Maybe you can live with this output already.

8 Comments

how to tweak the pattern to get parameters without quotes ?
@AakashChakravarthy: I added a tweak for those. However, I think if you want something really safe, this needs another update.
still more one issue, when the parameter value is empty, the output is not correct... what will be the tweak ?
Well it's correct, subindex 2 is an empty string. The only thing that differs is that there is an additional subindex 3. Is that really a problem? And leaving a note in case you need to have escape characters in values, this answer is showing some technique: @AakashChakravarthy: I added a tweak for those. However, I think if you want something really safe, this needs another update, see as well stackoverflow.com/questions/6243778/…
no thats not the problem... when tag param="" -----> param value is empty, in this case i get a varied o/p. i tried tweaking the (?:="?([^"]+)"?|()) section, but its in-vain.... do you have any idea ?
|
0

Maybe you're interested in this litte snippet that parses all sorts of attribute styles. <div class="hello" id=foobar style='display:none'> is valid html(5), not pretty, I know…

<?php
$string = '<tag2 display="users" limit="5">';

$attributes = array();
$pattern = "/\s+(?<name>[a-z0-9-]+)=(((?<quotes>['\"])(?<value>.*?)\k<quotes>)|(?<value2>[^'\" ]+))/i";
preg_match_all($pattern, $source, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
  $attributes[$match['name']] = $match['value'] ?: $match['value2'];
}

var_dump($attributes);

will give you

$attributes = array(
  'display' => 'users',
  'limit' => '5',
);

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.