[SOLVED] Explode Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bpgillett
Forum Newbie
Posts: 11
Joined: Mon Jul 26, 2004 3:25 am

Explode Question

Post by bpgillett »

hi. i want to add a prefix to each word inputed in a text field. the words are separated by spaces. i can use the explode function to separate the words. but i can't fugure out how to add a the prefix then implode them back into a string. the text input field may have varying numbers of words depending on the user's input. for example:

if $instruments = "perc voice pno" (as inputed by the user)

i want the new string, $instruments = "+perc +voice +pno"

any ideas.

thanks.



?>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

try the following

Code: Select all

$temp=explode (' ',$your_string);
$your_string='prefix'.implode('prefix',$temp);
The prefix is added to the first word by the 'prefix' before the implode.
bpgillett
Forum Newbie
Posts: 11
Joined: Mon Jul 26, 2004 3:25 am

Post by bpgillett »

thanks codergoblin. that works perfectly!

--brian
bpgillett
Forum Newbie
Posts: 11
Joined: Mon Jul 26, 2004 3:25 am

implode with suffix

Post by bpgillett »

i appologize for my lack of inference, but i'm now having trouble using this concept to also add a suffix. i tried:

Code: Select all

$temp=explode (' ',$instrumentation); 
$n_instrumentation=' +'.implode.'+' (' +',$temp,'+');
Last edited by bpgillett on Mon Jul 26, 2004 3:19 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$n_instrumentation = ' +' . implode(' +',$temp) . ' +';
bpgillett
Forum Newbie
Posts: 11
Joined: Mon Jul 26, 2004 3:25 am

trouble adding suffix with implode

Post by bpgillett »

thanks for the suggestion, feyd. it seems, however, to only add the suffix to the last word of the string. i am trying to add the suffix to all words in the string. (the string is a few words separated by spaces.)

the prefix gets added to all words nicely using:

Code: Select all

$temp=explode (' ',$your_string); 
$your_string='prefix'.implode('prefix',$temp);
thanks for any more suggestions.

--brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll have to walk through the array in order to do that then.
bpgillett
Forum Newbie
Posts: 11
Joined: Mon Jul 26, 2004 3:25 am

Post by bpgillett »

i'm not sure how to do that. do you mean write a loop that adds the suffix individually to each item in the array? if you could suggest how to code that, i would greatly appreciate it. i've been trying with no success. thanks.

--brian
bpgillett
Forum Newbie
Posts: 11
Joined: Mon Jul 26, 2004 3:25 am

Post by bpgillett »

i just used a foreach argument and it works. thanks though.

-brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

thinking about it, this is a possibility:

Code: Select all

$newstring = 'prefix'.implode('suffix' . ' ' . 'prefix', explode(' ',$oldstring)) . 'suffix';
bpgillett
Forum Newbie
Posts: 11
Joined: Mon Jul 26, 2004 3:25 am

Post by bpgillett »

wow. that does work. thanks for the help.

-brian
Post Reply