Easy question
Moderator: General Moderators
-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
Easy question
How do I get the first word of a string?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
$words = explode(' ', $string);
echo $words[0];-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
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.
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....- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
-
ChessclubFriend
- Forum Newbie
- Posts: 21
- Joined: Sun Mar 25, 2007 9:13 pm
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.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);
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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.
And thank you Z3RO21.
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.[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.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact: