2013-01-06 46 views
1

解決方案我在尋找: 我想重新排列文本字符串結果中的單詞,使作業標題從字符串的末尾移動到每個行項目的字符串的開頭。如何從MySQL查詢中重新排列字符串中的文本?

當前,我正在從外部醫療數據庫查詢中檢索數據($查詢)。但是,我不能對數據庫或MySQL查詢語句本身進行任何更改。

$查詢被檢索,然後我把在$數據結果通過以下命令陣列:

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

我然後改變所有的職稱在$數據大寫陣列如下:

$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])); 
} 

的最終輸出是在下面的行項的文本字符串格式:

實例查詢的結果(4行項目)

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 

我想重新排列這些結果使得我可以將數據輸出到我的用戶以下面的格式通過移動職務到所述開始每一行項目的如:

期望的結果(通常超過1000個項目)

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 

理想的情況下,如果可能的話,這也將是不錯的的歲時移居到開始的文本字符串結果如下:

理想的效果

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) 
+1

而不是編號變量,請考慮使用數組。 PHP有他們 - 多麼實用! - 'str_replace'或'preg_replace'和朋友也支持這些!這將幫助您更快速地將其中的一部分整理到秩序中,以便更好地集中注意力,以便不打字。 – hakre

+0

什麼是您的表格結構以及您使用什麼查詢來從中進行選擇? – GordonM

+0

@GordonM目前,我正在從外部醫療數據庫查詢($查詢)中檢索數據。但是,我不能對數據庫或MySQL查詢語句本身進行任何更改。 – Sammy

回答

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.   
} 
+0

謝謝,我會試試看。 – Sammy

+0

幾乎可以工作 - 我注意到,如果字符串不包含任務標題或年齡結果是「年 - - 」和文本字符串的其餘部分被刪除。 – Sammy

+0

當然,我使用的正則表達式不能處理丟失的部分,您能否提供一些代碼,以顯示文本行的所有可能的異常/變體? –