简体   繁体   English

如何从MySQL查询重新排列字符串中的文本?

[英]How do you rearrange text within a string from a MySQL query?

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 ). 目前,我正在从外部医疗数据库查询( $ query )中检索数据。 However, I cannot make any changes to the database or to the MySQL query statement itself. 但是,我无法对数据库或MySQL查询语句本身进行任何更改。

The $query is retrieved and I then place the results in a $data array via the following command: 检索$ query ,然后通过以下命令将结果放入$ data数组中:

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

I then change all the job titles to uppercase in the $data array as follows: 然后,我将$ data数组中的所有职位更改为大写,如下所示:

$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) 查询结果示例(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

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) 所需结果(通常超过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

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)

You could use a regular expression to extract and rearrange the array: 您可以使用正则表达式提取并重新排列数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM