I would like to arrange html tags in php.

Asked 2 years ago, Updated 2 years ago, 78 views

$str='abcdefg<span style="font-size:20px;">hijklmnop</span>qrs<span style="font-size:18px;color=F00;">tuv</span>wxyz';;

I would like to use php to make the above string as follows.

Array([0]=>abcdefg,[1]=>span style="font-size:20px;">hijklmnop</span>,[2]=>qrs,[3]=>span style="font-size="font-size:18px;color"

How can I resolve this?

php html array

2022-09-30 21:19

2 Answers

If you don't think about nesting, you can use regular expressions.

$str='abcdefg<span style="font-size:20px;">hijklmnop</span>qrs<span style="font-size:18px;color=F00;">tuv</span>wxyz';;
preg_match_all('/(';[^>]+>[^>]+<\/[^>]+>|[^<]+')/',$str,$matches);
print_r($matches[1]);

As a regular expression concept,

  • Getting Between <hoge>~</hoge>
  • Get up to <

This will give you the following results:

Array
(
    [0] =>abcdefg
    [1] =><span style="font-size: 20px;">hijklmnop</span>
    [2] =>qrs
    [3] =><span style="font-size: 18px;color=F00;">tuv</span>
    [4] =>wxyz
)


2022-09-30 21:19

If php deals with html like jquery, there is something called phpquery.
http://qiita.com/zaburo/items/465ca691aebad2b5691e


2022-09-30 21:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.