0

I have a string in this form :

<strong>dsds </strong><em>sdqsd </em><span style='text-decoration: underline;'>test</span>

and i want to put it in an associative array :

array('strong' => 'dsds' , 'em' => 'sdqsd' , 'underline' => 'test');

in a way that the order of elements int this array have to be the same as their order in the string .

which means if i have for example :

<span style='text-decoration: underline;'>test</span><strong>dsds </strong><em>sdqsd </em>

i get :

 array( 'underline' => 'test', 'strong' => 'dsds' , 'em' => 'sdqsd');

I tried doing it by using strpos in a loop but that's a lot of iterations specialy if i have a big string, so is there a simpler way using regex ?

Thanks.

Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
  • 1
    Your question is not clear about how deep you want to go. What is the expected results of: `abcdef` ? A RegEx would be unsuitable for this, you will need an HTML parser instead. – Jeff Lambert Apr 25 '12 at 13:22
  • This definitely falls under the umbrella of parsing HTML with regex, which as we all know (obligatory link) [you should not do](http://stackoverflow.com/questions/1732348)... – DaveRandom Apr 25 '12 at 13:26
  • thanks for the suggestion , i will try php's DOMDocument but still i will have a problem with preserving the order – Mouna Cheikhna Apr 25 '12 at 13:28

1 Answers1

1

1, You can use tag names as array indexes only in case you have only one tage of each type per parsed HTML statement.

2, Maybe this library will help: http://simplehtmldom.sourceforge.net/

3, Or create custom parser using regular expression. That's solution that I really wont suggest because of big CPU performance requirements.

castor
  • 585
  • 2
  • 8