简体   繁体   English

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

[英]sorting out words inside tags in a string with php

I have a list of HTML tags, that i need to re-order into a list item (html). 我有一个HTML标签列表,我需要重新排序到列表项(html)。 By using PHP I'm trying to get all the words that's contained inside an tag and tag. 通过使用PHP,我试图获取标签和标签中包含的所有单词。

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? 如果我只想提取dt和dd标签? how can i do this with php? 我怎么能用PHP做到这一点? and reorder them all into a < li > tag instead... 并将它们全部重新排序到<li>标签中......

If you are retrieving a database record and then printing the results with PHP, you can order the search. 如果要检索数据库记录,然后使用PHP打印结果,则可以订购搜索。

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

If you are trying to read the DOM, you would need to use Javascript. 如果您尝试读取DOM,则需要使用Javascript。 And that is a lot more complicated of an answer. 这是一个更复杂的答案。

  1. Get & store each DOM object 获取并存储每个DOM对象
  2. Order them by some determinant 按一些决定因素排序
  3. Erase the current DOM 擦除当前的DOM
  4. Print to the DOM the new ordered list 向DOM打印新的有序列表

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/ '); //从URL或文件创建DOM $ html = file_get_html(' http://www.google.com/ ');

// Find all images foreach($html->find('img') as $element) echo $element->src . //找到所有图像foreach($ html-> find('img')as $ element)echo $ element-> src。 '
'; “;

// Find all links foreach($html->find('a') as $element) echo $element->href . //查找所有链接foreach($ html-> find('a')as $ element)echo $ element-> href。 '
'; “;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM