使用php整理字符串中的标签内的单词

I have a list of HTML tags, that i need to re-order into a list item (html). By using PHP I'm trying to get all the words that's contained inside an tag and tag.

If there any way to order this correctly?

Example of the string itself

$string = "
<div>
<dt>Question #1<dt>
<dd>Answer to #1</dd>
<dt>Question #2<dt>
<dd>Answer to #2</dd>
</div>
";

If i wanted to extract only the dt and dd tags? how can i do this with php? and reorder them all into a < li > tag instead...

If you are retrieving a database record and then printing the results with PHP, you can order the search.

$sql = mysqli_query("SELECT * FROM tablename ORDER BY column_name");

If you are trying to read the DOM, you would need to use Javascript. And that is a lot more complicated of an answer.

  1. Get & store each DOM object
  2. Order them by some determinant
  3. Erase the current DOM
  4. Print to the DOM the new ordered list

I think you will need to be more descriptive of what you are trying to do, in order to find the right answer.

// Create DOM from URL or file $html = file_get_html('http://www.google.com/');

// Find all images foreach($html->find('img') as $element) echo $element->src . '
';

// Find all links foreach($html->find('a') as $element) echo $element->href . '
';

Taken from http://simplehtmldom.sourceforge.net/