Jump to content

split field entries


jesushax

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/106607-split-field-entries/
Share on other sites

 

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.

Link to comment
https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546431
Share on other sites

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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/106607-split-field-entries/#findComment-546438
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.