How to split an HTML string into chunks in PHP? [d

2019-06-14 21:32发布

This question already has an answer here:

I would like to split a string efficiently into different sub strings like this :

  <li><a href="">One</a></li><li><a href>Two</a></li><li><a href>Three</a></li>.....<li><a herf>Last</a></li>

I would like to split this string in 3 parts and put each string in a variable.

The first variable $first should contain the first li (<li>One</li>) The second variable $second should contain the rest expect the last (<li>Two</li><li>Three</li>.....) The third variable $third should contain the last li (<li>Last</li>)

Anyone can help ?

标签: php substring
3条回答
放我归山
2楼-- · 2019-06-14 22:18

you can try with explode

see code example:

<?php
// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *

?>
查看更多
三岁会撩人
3楼-- · 2019-06-14 22:23

Should work:

<?php 

$dom = new DOMDocument;
$dom->loadHTML('<li><a href="#">One</a></li><li><a href="#">Two</a></li><li><a href="#">Three</a></li><li><a href="#">Last</a></li>');
$liTags = $dom->getElementsByTagName('li');

$chunks = array();
foreach ($liTags as $li) {
    $chunks[] = '<li>'.strip_tags($li->nodeValue).'</li>';
}

list($first,$second,$third) = array(
   array_slice($chunks,0,1),
   array_slice($chunks,1,count($chunks)-2),
   array_slice($chunks,-1),    
);


?>
查看更多
Rolldiameter
4楼-- · 2019-06-14 22:35

The best thing to do here, because you're dealing with markup, is to treat the data as what it is: HTML. Parse the DOM tree, iterate over the nodes you're interested in and get the data like so:

$dom = new DOMDocument;
$dom->loadHTML($data);//parses the markup string
$li = $dom->getElementsByTagName('li');
$liValues = [];
foreach ($li as $elem) {
    $liValues[] = $elem->textContent; // not nodeValue if the nodes can contain other elements, like in your case an "a" tag
}

To get the first, last and middle elements, just write this:

$length = $li->length; // or count($li); to get the total number of li's
$nodes = [
    $li->item(0), //first
    $li->item(round($length/2)), // middle
    $li->item($length -1), // last
];
//or, given that you don't need the actual nodes
$nodes = [
    $li->item(0)->textContent,
    $li->item(round($length/2))->textContent,
    $li->item($length -1)->textContent,
];

Or just get the parent node (ul or ol), and use $parent->firstChild and $parent->lastChild to get the first and last elements in the list, as CD001 suggested in the comments.

Useful links:

You can use the attributes property of the DOMNode elements in the DOMNodeList returned by getElementsByTagName to further exapand on the data you store

查看更多
登录 后发表回答