jesushax Posted May 21, 2008 Share Posted May 21, 2008 hi i have a field in my mysql db in this filed it will have items like this 'item1''item2''item3''item4' can someone show me how to use the split function to split the words between the '' so i actaully only want the words item1 item2 item3 item4 then how to print those words like <li>item1</li> <li>item2</li> <li>item3</li> <li>item4</li> thanks alot for any help Quote Link to comment https://forums.phpfreaks.com/topic/106607-split-field-entries/ Share on other sites More sharing options...
thebadbad Posted May 21, 2008 Share Posted May 21, 2008 <?php $str = "'item1''item2''item3''item4'"; $str = trim($str, "'"); $strings = explode("''", $str); foreach ($strings as $value) { echo "<li>$value</li>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546394 Share on other sites More sharing options...
phpzone Posted May 21, 2008 Share Posted May 21, 2008 <?php $string = "'item1''item2''item3''item4'"; $result = preg_replace("/'(.*?)'/m", '<li>\1</li>', $string); echo $result . '<br />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546395 Share on other sites More sharing options...
jesushax Posted May 21, 2008 Author Share Posted May 21, 2008 thanks alot, could you show me how it works i like to know how things work so i can recreate situations again if needed thanks Quote Link to comment https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546428 Share on other sites More sharing options...
phpzone Posted May 21, 2008 Share Posted May 21, 2008 The best way to find out is to read the php.net manual http://www.php.net/funcref http://www.php.net/langref or http://www.hudzilla.org/ and look up Regular Expressions: http://www.regular-expressions.info/ if you want to learn the preg_replace way. Quote Link to comment https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546431 Share on other sites More sharing options...
thebadbad Posted May 21, 2008 Share Posted May 21, 2008 I'll give you some quick comments on my method: <?php $str = "'item1''item2''item3''item4'"; // trim off the single quotes at the start and the end of $str $str = trim($str, "'"); // split the string at every two single quotes, and get an array of the items without any quotes $strings = explode("''", $str); // loop through the array of items, and output each item ($value) inside <li> tags, while appending a newline (\n) foreach ($strings as $value) { echo "<li>$value</li>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546438 Share on other sites More sharing options...
jesushax Posted May 21, 2008 Author Share Posted May 21, 2008 ahhh i see, i like it thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546451 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.