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
big_c147
Forum Newbie
Posts: 15 Joined: Wed Jun 22, 2005 9:32 am
Post
by big_c147 » Thu Jun 23, 2005 10:33 am
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
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Thu Jun 23, 2005 8:25 pm
preg_replace()
aaronhall
DevNet Resident
Posts: 1040 Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:
Post
by aaronhall » Thu Jun 23, 2005 8:55 pm
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
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Thu Jun 23, 2005 8:57 pm
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.
aaronhall
DevNet Resident
Posts: 1040 Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:
Post
by aaronhall » Thu Jun 23, 2005 8:59 pm
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
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Thu Jun 23, 2005 9:01 pm
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...
aaronhall
DevNet Resident
Posts: 1040 Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:
Post
by aaronhall » Thu Jun 23, 2005 9:14 pm
Couldn't tell you - I lost the timer I had that records microseconds. Perhaps you could, dethron!
harrison
Forum Commoner
Posts: 30 Joined: Thu Jun 09, 2005 12:23 am
Post
by harrison » Thu Jun 23, 2005 9:22 pm
Code: Select all
$wordArray = explode(' ',$passedVar);
// instead of looping, use the reverse
$newSentence = "*".implode("*,*",$wordArray)."*";
harrison
Forum Commoner
Posts: 30 Joined: Thu Jun 09, 2005 12:23 am
Post
by harrison » Thu Jun 23, 2005 9:27 pm
Or better
Code: Select all
$newSentence = '*'.str_replace(' ','*,*',$passedVar).'*';
But I like my girlfriend most
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Thu Jun 23, 2005 9:37 pm
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...
harrison
Forum Commoner
Posts: 30 Joined: Thu Jun 09, 2005 12:23 am
Post
by harrison » Thu Jun 23, 2005 9:40 pm
experimenting is the mother of learning...(who said that?)
Anyway, I always try the code before posting.
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Thu Jun 23, 2005 9:43 pm
so how did you get the difference? I mean it must be miliseconds that aaronhall mentioned.
harrison
Forum Commoner
Posts: 30 Joined: Thu Jun 09, 2005 12:23 am
Post
by harrison » Thu Jun 23, 2005 9:46 pm
I think it is enough that we answered the poor man's problem. Any 'testing of skills' will be perfect on another thread.
dethron
Forum Contributor
Posts: 370 Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul
Post
by dethron » Thu Jun 23, 2005 9:49 pm
right.