Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract 1st word from mysql database field

Status
Not open for further replies.

overflo

Technical User
Feb 10, 2003
70
AU
I need to get the first word from a field and separate it from the rest of the words and have no idea where to start. Any help would be greatly appreciated.
 
And I need to put the firstword into a variable and also any other words after that.
i.e. $firstword =
$rest =
 
worked it out. Here it is if anyone wants to know
Code:
//tell it what character we're looking for
//i.e. space and take the string up to that
$position = strpos($title, ' ');
if ($position !== FALSE){
$first = substr ($title, 0, $position);
}else{
//in case there is only one word
       $first = $title;
}
// now take the string from after the first space
       $rest = strstr($title, ' ');
 
from mysql I'd have used a query like this:
Code:
select *,substring(title,1,instr(title,' ')) as titleword   from mytable;

This will return a result set of all the fields in the table and a field created from the first word in `title`.

Then all you do is standard while loop to step through the results and display them...

MySQL questions are always best asked in here but your solution is oddly php based when you asked for mysql solution :)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top