简体   繁体   中英

PHP limit string to x then cut li and store it in a value for later use

I have this html as a description

<p>Intro Description here</p>
<ul>
<li>Magnetic numbers are perfect for steel safety scoreboards.</li>
<li>Numbers are white and are 20-mil thick and 2" high. </li>
<li>Sold in full packages of 10 numbers (0-9).</li>
</ul>

I would like to have like say 90 character limits, so in this particular example, It ends in the second <li> . How can I cut and store (it in a variable for later use, same page) the second and third <li> completely ignoring the 90 character limit. I mean, if the 90th character falls under the second <li> , that <li> and its content will be cut off and stored in a variable.

you should try to get ul elements like this:

$u = $dom->getElementsByTagName("ul"); 

Get inner html

$str = $u->saveHTML() ;

Complete code (assuming that you only want parts inside <ul> </ul> tags)

$html = "Your html string"
$dom = new domDocument; 
$dom->loadHTML($html);
$u = $dom->getElementsByTagName("ul"); 
$str = $u->saveHTML() ;
$string = substr($str,0,90)  //getting only 90 chars

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM