0 レビュー
5 回答
phpを使用してリストを配列に変換します。どのように?
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で始まるリンクのみを表示したい。 この説明が私の質問をよりよく理解するのに役立つことを願っています
わからない
0
レビュー
答え :
解決策:
<?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
わからない
0
レビュー
答え :
解決策:
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
)
わからない
0
レビュー
答え :
解決策:
$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
}
わからない
0
レビュー
答え :
解決策:
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;
}
}
わからない
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。