开发者

Insert into an array

开发者 https://www.devze.com 2023-02-03 06:02 出处:网络
$alltext variable contents this code inside: <li>sometext</开发者_如何学Cli><li>sometext</li>

$alltext variable contents this code inside:

<li>sometext</开发者_如何学Cli><li>sometext</li>
<li>sometext</li><li>sometext</li>

How do I get each <li>...</li> like an item of an array?


If each element is separated by a "new line", you could try:

$array = explode("\n",$alltext);

If they aren't, then:

$array = explode("</li>");

foreach($array as $k=>$v){
   $array[$k] = $v."</li>";
}


$array = explode("\n", $alltext);

It seems like you should have a better approach, though.


If text is separated by new line, you could do this:

$arr = explode("\r\n", $alltext);
foreach($arr as $val){
  echo $val;
}

You would use \n if not under Windows.


Same answer as from @Sarfraz and @Dave Kiss, but crossplatform:

$listItems = explode(PHP_EOL, $alltext);

However, I agree with Dave Kiss when he says you should have a better approach (in software design).


use explode function, use '\r\n' (linebreak) as a splitter

0

精彩评论

暂无评论...
验证码 换一张
取 消