获取ul li一个字符串值并将它们存储在变量或数组php中 [英] Get ul li a string values and store them in a variable or array php

查看:107
本文介绍了获取ul li一个字符串值并将它们存储在变量或数组php中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的网站上的列表项的字符串值存储在PHP中的变量/数组中,以便对它们进行一些条件性检查/语句。使用PHP获取列表项的字符串值有困难,有人可以帮助吗?



这是标记。

 < div class =coursesListed> 
< ul>
< li>< a href =#>< h3>项目1< / h3>< / a>< / li>
< li>< a href =#>< h3>项目2< / h3>< / a>< / li>
< li>< a href =#>< h3>项目3< / h3>< / a>< / li>
< / ul>
< / div>

我想要的是一个变量或数组,它包含值Item one,Item两个,项目三。

解决方案

尝试这个

  $ html ='< div class =coursesListed> 
< ul>
< li>< a href =#>< h3>项目1< / h3>< / a>< / li>
< li>< a href =#>< h3>项目2< / h3>< / a>< / li>
< li>< a href =#>< h3>项目3< / h3>< / a>< / li>
< / ul>
< / div>';

$ doc = new DOMDocument();
$ doc-> loadHTML($ html);
$ liList = $ doc-> getElementsByTagName('li');
$ liValues = array();
foreach($ liList as $ li){
$ liValues [] = $ li-> nodeValue;
}

var_dump($ liValues);


Im trying to store the string value's of a list item on my website into a variable/array in PHP to do some conditional checks/statements with them. Am having a bit off difficulty getting the list item's string value using PHP, can anybody help?

This is the markup.

<div class="coursesListed">
<ul>
<li><a href="#"><h3>Item one</h3></a></li>
<li><a href="#"><h3>item two</h3></a></li>
<li><a href="#"><h3>Item three</h3></a></li>            
</ul>
</div>

What i want ideally is either a variable or array that holds the values "Item one", "Item two", "Item three".

解决方案

Try this

$html = '<div class="coursesListed">
<ul>
<li><a href="#"><h3>Item one</h3></a></li>
<li><a href="#"><h3>item two</h3></a></li>
<li><a href="#"><h3>Item three</h3></a></li>            
</ul>
</div>';

$doc = new DOMDocument();
$doc->loadHTML($html);
$liList = $doc->getElementsByTagName('li');
$liValues = array();
foreach ($liList as $li) {
    $liValues[] = $li->nodeValue;
}

var_dump($liValues);

这篇关于获取ul li一个字符串值并将它们存储在变量或数组php中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆