1

Solution I am looking for: I would like to rearrange words within the text string results such that the job title is moved from the end of the string to the beginning of the string for each line item.

Currently, I am retrieving data from an external medical database query ($query). However, I cannot make any changes to the database or to the MySQL query statement itself.

The $query is retrieved and I then place the results in a $data array via the following command:

while($row = mysql_fetch_assoc($query)){$data[] = $row;}

I then change all the job titles to uppercase in the $data array as follows:

$job_01 = 'anesthesiologist';
$job_02 = 'dentist';
$job_03 = 'general practitioner';
$job_04 = 'internist';
$job_05 = 'lawyer';
$job_06 = 'manager';
$job_07 = 'pediatrician';
$job_08 = 'psychiatrist';

$replace_01 = 'ANESTHESIOLOGIST';
$replace_02 = 'DENTIST';
$replace_03 = 'GENERAL PRACTITIONER';
$replace_04 = 'INTERNIST';
$replace_05 = 'LAWYER';
$replace_06 = 'MANAGER';
$replace_07 = 'PEDIATRICIAN';
$replace_08 = 'PSYCHIATRIST';

$searchArray = array($job_01, $job_02, $job_03, $job_04, $job_05, $job_06, $job_07, $job_08);
$replaceArray = array($replace_01, $replace_02, $replace_03, $replace_04, $replace_05, $replace_06, $replace_07, $replace_08);

for ($i=0; $i<=count($data)-1; $i++) {
 $line[$i] = str_ireplace($searchArray, $replaceArray, $data[$i]));
}

The final output is in the following line item text string format:

Example Query results (4 line items)

California Long time medical practitioner  -  ANESTHESIOLOGIST 55yr
New York Specializing in working with semi-passive children - PEDIATRICIAN (doctor) 42yr
Nevada Currently working in a new medical office - PSYCHIATRIST 38yr
Texas Represents the medical-liability industry - LAWYER (attorney) 45yr

I would like to rearrange these results such that I can output the data to my users in the following format by moving the job title to the beginning of each line item as in:

Desired results (usually over 1000 items)

ANESTHESIOLOGIST - California Long time medical practitioner  - 55yr
PEDIATRICIAN - New York Specializing in working with semi-passive children - (doctor) 42yr
PSYCHIATRIST - Nevada Currently working in a new medical office - psychiatrist 38yr
LAWYER - Texas Represents the medical-liability industry - lawyer (attorney) 45yr

Ideally, if possible, it would also be nice to have the age moved to the beginning of the text string results as follows:

Ideal Results

55yr - ANESTHESIOLOGIST - California Long time medical practitioner
42yr - PEDIATRICIAN - New York Specializing in working with semi-passive children - (doctor)
38yr - PSYCHIATRIST - Nevada Currently working in a new medical office - psychiatrist
45yr - LAWYER - Texas Represents the medical-liability industry - lawyer (attorney)
4

1 回答 1

1

您可以使用正则表达式来提取和重新排列数组:

for ($i=0; $i<=count($data)-1; $i++) {
    $line[$i] = str_ireplace($searchArray, $replaceArray, $data[$i]));
    // variant a, complete line
    if(preg_match_all('/(.*)\s+-\s+(.*)\s+(\d+)yr$/', $line[$i],$matches)) {
        $line[$i] = $matches[3][0].'yr - '.$matches[2][0].' - '.$matches[1][0];
    // variant b, a line with age, but no jobtitle
    } elseif(preg_match_all('/(.*)\s+-\s+(\d+)yr$/', $line[$i],$matches)) {
        $line[$i] = $matches[2][0].'yr - '.$matches[1][0];
    // variant c, no age
    } elseif(preg_match_all('/(.*)\s+-\s+(.*)$/', $line[$i],$matches)) {
        $line[$i] = $matches[2][0].' - '.$matches[1][0];
    }
    // in other cases (no age, no jobtitle), the line is not modified at all.        
}
于 2013-01-06T10:48:34.920 回答