Easy question

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
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Easy question

Post by ChessclubFriend »

How do I get the first word of a string?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$words = explode(' ', $string); 
echo $words[0];
:wink:
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

How do I delete the first word of a string? :)

Thanks for the explode function, will come in handy :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Last edited by John Cartwright on Tue May 08, 2007 10:11 pm, edited 1 time in total.
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

Can I convert a string into an array?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Can you tell us what you are trying to do actually ??
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post 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....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Last edited by John Cartwright on Tue May 08, 2007 10:12 pm, edited 1 time in total.
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

you already said that idiot
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post 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);
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You just insulted an "Extreme Guru Moderator" for pointing out your error. I think you just redefined the term "idiot" :rofl:
Post Reply