Page 1 of 1

Break a string into multiple strings

Posted: Tue Mar 24, 2009 4:16 pm
by Anarking
Good day


I got this string that I want to break into different smaller strings

For example

I want to add this to a drop down select menu
Column B|b1|b2|b3|b4|b5|

Where the | represents the end of an element

So, how do I break each part off and add them as options?

Re: Break a string into multiple strings

Posted: Tue Mar 24, 2009 4:34 pm
by requinix

Re: Break a string into multiple strings

Posted: Tue Mar 24, 2009 4:39 pm
by Anarking
Ah, much love buddy.

Re: Break a string into multiple strings

Posted: Tue Mar 24, 2009 4:43 pm
by requinix
Are you reading this from a file? Because fgetcsv will both read and split the line.

Re: Break a string into multiple strings

Posted: Tue Mar 24, 2009 4:51 pm
by Anarking
Nah, reading it from an excel file at the moment but I gotta add in some CSV functionality later so thats gonna help

Re: Break a string into multiple strings

Posted: Tue Mar 24, 2009 6:29 pm
by tech603
From php.net http://us2.php.net/manual/en/function.explode.php

<?php
$str = 'one|two|three|four';

// positive limit
print_r(explode('|', $str, 2));

// negative limit (since PHP 5.1)
print_r(explode('|', $str, -1));
?>

The above example will output:

Array
(
[0] => one
[1] => two|three|four
)
Array
(
[0] => one
[1] => two
[2] => three
)

Hope this helps.

Matthew Vass
QA Analyst
mvass@hostmysite.com
http://www.hostmysite.com?utm_source=bb