【问题标题】:Convert list into an array using php. How?使用 php 将列表转换为数组。如何?
【发布时间】:2012-03-03 15:12:00
【问题描述】:

我有一个名为listelements() 的函数,它输出类似<li>text1</li><li>text2</li> 的文本。

我有 500 多个元素。现在我想把它转换成一个数组。

谁能帮帮我?谢谢。

注意:我使用的是 php

更新:

我想要实现的是字母导航。截至目前,我的函数按列表顺序显示链接。而不是我想把它保存在一个数组中。然后我想用字符过滤它们。

$valid_characters = range( 'a' , 'z' );
$valid_numbers = array(1,2,3,4,5,6,7,8,9,0);

当用户单击“A”时,我只想显示以 A 开头的链接。 希望这个解释可以帮助你们更好地理解我的问题

【问题讨论】:

  • 你期望的输出数组是什么?
  • listelemets() 的输入是什么?使用它,它可能本身就是一个数组。
  • 您只需要文本还是带有li 标签?
  • 只修改函数以输出数组而不是html代码不是更容易吗?

标签: php arrays string list


【解决方案1】:
<?php
$output = listelements();
$array = explode("<li>", $output);

//First element will be empty, so remove it
unset($array[0]);

// Now remove "</li>" at end of input
array_walk($array, create_function('&$val', '$val = str_replace("</li>", "", $val)'));

// $array should now contain your elements

【讨论】:

  • 如果 2 li-s 之间有什么东西,这不能正常工作。
  • 当然可以,但是 OP 将格式指定为"&lt;li&gt;$element&lt;/li&gt;" 的多个连接,所以没有问题。
【解决方案2】:

explode 不能很好地处理 html 标签(将它们视为多个分隔符)。

如果 CPU 时间不是问题,请尝试使用 preg_match,示例如下:

<?PHP

$input='<li>text1</li><li>text2</li><LI><p>text3</p></lI><Li>text fou4r</li>';

preg_match_all('(<(li|Li|LI|lI)>(.*)</(li|Li|LI|lI)>)siU', $input, $output);

print_r($output[2]);
?>


输出:

Array

    (

        [0] => text1
        [1] => text2
        [2] => <p>text3</p>
        [3] => text fou4r
    )

【讨论】:

  • 如果您的模式不区分大小写,为什么要匹配大小写的所有变体?即使那样,你为什么要这样做而不是[LlIi]
  • 我认为它比上面的 iblue 更好的解决方案...使用 regexp 而不是 explode 和 str_replace。
【解决方案3】:
$strarr=explode("<li>",$string); //Breaks every <li>
$i=(-1); //Makes -1 as array starts at 0
$arr=array(); //this will be your array
foreach($strarr as $expl){ //Go through all li's
$ai=explode("</li>",$expl);//Get between the li. If there is something between </li> and <li>, it won't return it as you don't need it
if($i!=(-1))$arr[$i]=$ai[0]; //First isn't valid
$i=$i+1; //add i plus one to make it a real array
}

【讨论】:

  • 这有语法错误; explode() 需要赋值,并且数组没有“新”关键字
  • 对不起,我的错,我总是忘记这一点。已更正。
【解决方案4】:

你可以使用 simple_html_dom https://simplehtmldom.sourceforge.io/manual.htm

require_once(./simple_html_dom.php);
$html = "<ul class=''><li class=''>Swagger transforms unfreshmen into legends of confidence</li><li class=''>Red Zone collection</li><li class=''>Enhances awesomeness</li><li class=''>Continue your confidence with Swagger Anti-perspirant & Deodorant and Body Spray</li><li class=''>Old Spice Red Zone Swagger Scent Men’s Bar Soap</li></ul>";

$result = li_to_array($html);
var_dump($result ); 

    
function li_to_array($str)
{
    if (is_string($str) && !empty($str)) {
        $html = str_get_html($str);
        $results= array();
        $response_results = $html->find('li');
        foreach ($response_results as $response_result){
            array_push($results, $response_result->plaintext);
        } 
        unset($html); //release memory

        return  $results;
    } else {
        return false;
    }
}

【讨论】:

    【解决方案5】:

    使用“preg_match”

    这里有更多详细信息:http://www.php.net/manual/en/function.preg-match-all.php

    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多