Heres the situation, i have a database of thousands of people names some there listed in different ways ( in example below Last is there last name, M is there middle name and First is there first name but all rows are actually different names ) row1= Last M First row2= Last M. First row3= Last Middle First row4= Last M- First what i want to do if possible is use something like str replace that would move First to the begining so i would echo something like First Last M
If they all have a middle name, you can split the string into three parts. While looping through the results. while($array = mysql_fetch_array($query)) { $name = explode(' ',$array); $first = $name[2]; $middle = $name[1]; $last = $name[0]; } PHP: If there is sometimes no middle name, or multiple spaces, you need to use preg_split and some logic to break everything out correctly.
please take note that names need to be consistent for the above method to work, any extra space will defeat the whole logic
If there's the posibility of multiple spaces, or no middle name, you need something like this: while(.....): $name = preg_split("/[\s]+/", $array); switch(count($name)): case 3: $first = $name[2]; $middle = $name[1]; $last = $name[0]; break; case 2: $first = $name[1]; $last = $name[0]; break; endswitch; endwhile; PHP:
call the rows out of the database .. change the row names to: first_name | middle_name | last_name edit your database name rows so it works like you want it to. then change the $row['first_name'] etc . into PHP vars. $first_name = $row['first_name']; $middle_name = $row['middle_name']; $last_name = $row['last_name']; make it do exactly what you want it to do with no confusion. Then you wont have to chang the tire in the rain on a muddy road.