azunoman Posted April 13, 2009 Share Posted April 13, 2009 I have the following mysql db field: $row_rs_pwc['ProdFeatures'.$lang] <ul> <li>Midi-Drive™ construction <li>User adjustable Anti-Tips <li>Com-For-Back™ Seating <li>Fully programmable controller <li>Precise quality control & 100% pre-ship inspection <li>Adjustable wheelbase length (Not available on Scout RF4) <li>Rigorous design testing <li>Mirror Glaze™ thru & thru color <li>Ergonomic design <li>Adjustable independent suspension <li>Ample horsepower <li>Three-level brake system <li>Multiple seating choices </ul> I need to dig out each <li> into an array so I coded the following: $listitems = explode('<li>',$row_rs_pwc['ProdFeatures'.$lang],-1); But when I dump the array it contains the <ul> at index 0. Why would it not put the </ul> into my array but the <ul>????? print_r($listitems); Array ( [0] => <ul> [1] => Midi-Drive™ construction [2] => User adjustable Anti-Tips [3] => Com-For-Back™ Seating [4] => Fully programmable controller [5] => Precise quality control & 100% pre-ship inspection [6] => Adjustable wheelbase length (Not available on Scout RF4) [7] => Rigorous design testing [8] => Mirror Glaze™ thru & thru color [9] => Ergonomic design [10] => Adjustable independent suspension [11] => Ample horsepower [12] => Three-level brake system Quote Link to comment https://forums.phpfreaks.com/topic/153857-explode-db-unorodered-list/ Share on other sites More sharing options...
premiso Posted April 13, 2009 Share Posted April 13, 2009 Because you are exploding at <li> not at </ul>. If you notice there is a <li> before the <ul> which splits that into a new variable. Since there is not a <li> before </ul> it just keeps it with the last index line. EDIT: To avoid this you can use strip_tags on the string before you parse it: $row_rs_pwc['ProdFeatures'.$lang] = strip_tags($row_rs_pwc['ProdFeatures'.$lang], '<li>'); Granted you would want to array_shift the first element off as it probably will contain a blank space. Quote Link to comment https://forums.phpfreaks.com/topic/153857-explode-db-unorodered-list/#findComment-808583 Share on other sites More sharing options...
azunoman Posted April 13, 2009 Author Share Posted April 13, 2009 It worked just as you explained above including the '?' value in index 0. Array ( [0] => [1] => Midi-Drive™ construction [2] => User adjustable Anti-Tips [3] => Com-For-Back™ Seating [4] => Fully programmable controller [5] => Precise quality control & 100% pre-ship inspection [6] => Adjustable wheelbase length (Not available on Scout RF4) [7] => Rigorous design testing [8] => Mirror Glaze™ thru & thru color [9] => Ergonomic design [10] => Adjustable independent suspension [11] => Ample horsepower [12] => Three-level brake system ) My question would be the '?' value for index 0?, if for example my db field only contained <li>'s and not the <ul>, I would shift off a valid <li>. To avoid the shift I tried checking the array after the explode to not print out a <li> if the value in index 0 wasr a ' ' (blank) and (also !is_null) but the echo of the <li> was executed so I get a ul with no text. if(" " != $listitems[$listitemindex]) { echo "<li>".$listitems[$listitemindex]; //display the <li> } Thanks for your time and patience. Getting this right will allow me to delete a field for each product, i.e. a shortened features field. I should have done it originally but I struggled then to. Quote Link to comment https://forums.phpfreaks.com/topic/153857-explode-db-unorodered-list/#findComment-808818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.