[SOLVED] Explode Question
Moderator: General Moderators
Explode Question
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.
?>
if $instruments = "perc voice pno" (as inputed by the user)
i want the new string, $instruments = "+perc +voice +pno"
any ideas.
thanks.
?>
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
try the following
The prefix is added to the first word by the 'prefix' before the implode.
Code: Select all
$temp=explode (' ',$your_string);
$your_string='prefix'.implode('prefix',$temp);implode with suffix
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$n_instrumentation = ' +' . implode(' +',$temp) . ' +';trouble adding suffix with implode
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:
thanks for any more suggestions.
--brian
the prefix gets added to all words nicely using:
Code: Select all
$temp=explode (' ',$your_string);
$your_string='prefix'.implode('prefix',$temp);--brian
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
thinking about it, this is a possibility:
Code: Select all
$newstring = 'prefix'.implode('suffix' . ' ' . 'prefix', explode(' ',$oldstring)) . 'suffix';