Page 1 of 1

Easy question

Posted: Mon May 07, 2007 9:13 pm
by ChessclubFriend
How do I get the first word of a string?

Posted: Mon May 07, 2007 9:18 pm
by John Cartwright

Code: Select all

$words = explode(' ', $string); 
echo $words[0];
:wink:

Posted: Mon May 07, 2007 9:49 pm
by ChessclubFriend
How do I delete the first word of a string? :)

Thanks for the explode function, will come in handy :)

Posted: Mon May 07, 2007 9:55 pm
by John Cartwright
a combination of explode() to get the words, unset() to delete the word, and finally implode() to rebuild the string.

Alternatively, you could do a preg_match() or preg_replace() to handle it in one swoop.

Posted: Mon May 07, 2007 10:19 pm
by ChessclubFriend
Can I convert a string into an array?

Posted: Mon May 07, 2007 11:06 pm
by John Cartwright

Posted: Tue May 08, 2007 12:46 am
by neel_basu
Can you tell us what you are trying to do actually ??

Posted: Tue May 08, 2007 9:03 pm
by ChessclubFriend
Thanks for your help guys. Getting there.

I converted a string to an array, used unset() to destroy the first word, and now I need to convert the array back to a string. Can I convert $words[] to a string? How do I convert the array back to $InputTextField.

Code: Select all

if($InputTextField)
   {
	$InputTextField = stripslashes($InputTextField);   // there might be slashes I don't want

	$words = explode(' ', $InputTextField);   // converts my string to an array
	$IsIt = $words[0];  // I dont know why I need this, I don't think i do
	unset($words[0]);  // destroys the first word in the array 
	$IsIt = $words[];   // tries to convert the array back to a string but there's an error!
	$InputTextField = $IsIt;  // Gives my string back to the original variable for further processing....

Posted: Tue May 08, 2007 9:06 pm
by John Cartwright
Jcart wrote:a combination of explode() to get the words, unset() to delete the word, and finally implode() to rebuild the string.

Posted: Tue May 08, 2007 9:21 pm
by ChessclubFriend
you already said that idiot

Posted: Tue May 08, 2007 9:28 pm
by ChessclubFriend
lucky for your sorry looking ego, I found the solution myself

Code: Select all

if($InputTextField)
   {
	$InputTextField = stripslashes($InputTextField);

	$words = explode(' ', $InputTextField);
	$IsIt = $words[0];
	unset($words[0]);
	$InputTextField = implode(" ", $words);

Posted: Tue May 08, 2007 9:48 pm
by Z3RO21
ChessclubFriend wrote:lucky for your sorry looking ego, I found the solution myself

Code: Select all

if($InputTextField)
   {
	$InputTextField = stripslashes($InputTextField);

	$words = explode(' ', $InputTextField);
	$IsIt = $words[0];
	unset($words[0]);
	$InputTextField = implode(" ", $words);
To bad for you, you just used the solution JCart gave you, this not only makes you look stupid but in my opinion calls for an apology from you to jcart he is just trying to help you but if you are to ignorant to take his aid you should look for another forum to help you and put up with your ignorant and rude behavior.

A hint for the future, if you don't understand a solution someone presents you with instead of attempting to look smart (and failing horribly) ask for clarification because I know JCart is more than willing to help anyone who asks for help / clarification.

Posted: Tue May 08, 2007 10:07 pm
by John Cartwright
How does that make me an idiot that I have to repeat myself, as the information you asked for I already gave you? I simply made it obvious what he answer was a second time so you wouldn't miss it again.

And thank you Z3RO21.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.3 wrote:15. A wide variety of opinions may be expressed on this public discussion forum. Respect positions which differ from your own. We like to see a vigorous intellectual discussion which proceeds politely and addresses the technical merits of different positions. Do not expect that anyone will alter their opinion of a topic regardless of however logical you believe your argument to be. Do not attack anyone personally for whatever reason - doing so contravenes the spirit in which this forum was founded and can have serious consequences.
Next time you break a rule, you banned. Nobody here has to put up with your insults... and don't expect me to ever help you again.

Posted: Tue May 08, 2007 10:15 pm
by Kieran Huggins
You just insulted an "Extreme Guru Moderator" for pointing out your error. I think you just redefined the term "idiot" :rofl: