Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.