Page 1 of 1
Seperate String
Posted: Thu Jun 23, 2005 10:33 am
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
Posted: Thu Jun 23, 2005 10:35 am
by John Cartwright
Moved to PHP-Code.
Posted: Thu Jun 23, 2005 8:25 pm
by dethron
preg_replace()
Posted: Thu Jun 23, 2005 8:55 pm
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
Posted: Thu Jun 23, 2005 8:57 pm
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.
Posted: Thu Jun 23, 2005 8:59 pm
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
Posted: Thu Jun 23, 2005 9:01 pm
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...
Posted: Thu Jun 23, 2005 9:14 pm
by aaronhall
Couldn't tell you - I lost the timer I had that records microseconds. Perhaps you could, dethron!

Posted: Thu Jun 23, 2005 9:22 pm
by harrison
Code: Select all
$wordArray = explode(' ',$passedVar);
// instead of looping, use the reverse
$newSentence = "*".implode("*,*",$wordArray)."*";
Posted: Thu Jun 23, 2005 9:27 pm
by harrison
Or better
Code: Select all
$newSentence = '*'.str_replace(' ','*,*',$passedVar).'*';
But I like my girlfriend most

Posted: Thu Jun 23, 2005 9:37 pm
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...
Posted: Thu Jun 23, 2005 9:40 pm
by harrison
experimenting is the mother of learning...(who said that?)
Anyway, I always try the code before posting.
Posted: Thu Jun 23, 2005 9:43 pm
by dethron
so how did you get the difference? I mean it must be miliseconds that aaronhall mentioned.
Posted: Thu Jun 23, 2005 9:46 pm
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.

Posted: Thu Jun 23, 2005 9:49 pm
by dethron
right.
