Seperate String

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
big_c147
Forum Newbie
Posts: 15
Joined: Wed Jun 22, 2005 9:32 am

Seperate String

Post by big_c147 »

Hi. I have a field on a form which is posted to another page. However this field is usually a senstance. What i want to be able to do is split each of the words they type and put a * infront and behind them and seperate them with a comma. This whould then be put back into a single string

for example....

if the user types "My Name is Craig"

i want this to be converted to

Code: Select all

$newstring = "*My*,*Name*,*is*,*Craig*" ;
Can anyone tell me how to do this

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP-Code.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

preg_replace()
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Alternative to preg_replace:

Code: Select all

$wordArray = split(' ',$passedVar);
foreach($wordArray as $key => $value) {
$newSentence .= "*" . $value . "*,";
}

$newSentence = trim($newSentence, ","); // trim off the trailing comma
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Tip of the day (from http://www.php.net/manual/en/function.split.php)

preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Ultra optimized:

Code: Select all

$wordArray = explode(' ',$passedVar);
foreach($wordArray as $key => $value) {
$newSentence .= "*" . $value . "*,";
}
 
$newSentence = trim($newSentence, ","); // trim off the trailing comma
Thanks dethron
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

accepted :) it is faster to use explode().
can you tell me a faster method to get rid of the last ","?
i am sure you can...
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Couldn't tell you - I lost the timer I had that records microseconds. Perhaps you could, dethron! :D
User avatar
harrison
Forum Commoner
Posts: 30
Joined: Thu Jun 09, 2005 12:23 am

Post by harrison »

Code: Select all

$wordArray = explode(' ',$passedVar);
// instead of looping, use the reverse
$newSentence = "*".implode("*,*",$wordArray)."*";
User avatar
harrison
Forum Commoner
Posts: 30
Joined: Thu Jun 09, 2005 12:23 am

Post by harrison »

Or better

Code: Select all

$newSentence = '*'.str_replace(' ','*,*',$passedVar).'*';
But I like my girlfriend most :wink:
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Dude,

Code: Select all

$newSentence = "*".implode("*,*",$wordArray)."*";


is good but i am not sure about the

Code: Select all

$newSentence = '*'.str_replace(' ','*,*',$passedVar).'*';


i checked manuel for string but i havent found any efficiency difference between (") and ('). They are all strings :(. If you found something let me know ;)
Happy coding...
User avatar
harrison
Forum Commoner
Posts: 30
Joined: Thu Jun 09, 2005 12:23 am

Post by harrison »

experimenting is the mother of learning...(who said that?)
Anyway, I always try the code before posting.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

so how did you get the difference? I mean it must be miliseconds that aaronhall mentioned.
User avatar
harrison
Forum Commoner
Posts: 30
Joined: Thu Jun 09, 2005 12:23 am

Post by harrison »

I think it is enough that we answered the poor man's problem. Any 'testing of skills' will be perfect on another thread. :roll:
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

right. :oops:
Post Reply