将匹配位置的所有匹配字符串替换为第一个符号

时间:2015-11-02 11:57:43

标签: php preg-replace preg-match substr

我有一个包含$lang关键字的查询字符串,现在我想在$lang之前替换所有内容,直到第一次出现'符号,并使用循环播放另一个数组的内容。

以下是我的查询字符串:

select 
COUNT(t1.id) as total, 
COUNT(t1.id) as 'total_$lang', 
CASE t2.`precence`
        WHEN '0' THEN 'Absent'
        WHEN '1' THEN 'Present'
        ELSE 'Unknown'
    END AS series,
CONCAT(DAYNAME(t1.date),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS ticks,
CONCAT(DATE_FORMAT(t1.date, '%a'),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS abv,
    courses.no AS label,
CASE t2.`precence`
        WHEN '0' THEN 'absent_$lang'
        WHEN '1' THEN 'present_$lang'
        ELSE 'unknown_$lang'
    END AS 'attendance_state_$lang',
CONCAT(DAYNAME(t1.date),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS 'attendance_date_$lang',
CONCAT(DATE_FORMAT(t1.date, '%a'),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS 'attendance_date_abv_$lang',
courses.no AS 'course_num_$lang'

from course_attendances as t1 left join `student_attendances` as t2 on t2.`course_attendance_id` = t1.`id`
right join courses ON courses.id = t1.course_id
where t1.`course_id` = '1' AND t1.date BETWEEN '2015-01-01' AND '2015-11-02'
group by t1.`date`,t2.`precence`

现在我想替换下面的数组

的内容
Array
(
    [total_en] => Total
    [absent_en] => Absent
    [present_en] => Present
    [unknown_en] => Unknown
    [attendance_state_en] => Attendance State
    [attendance_date_en] => Attendance Date
    [attendance_date_abv_en] => Attendance Date (abv)
    [course_num_en] => Course Number
)

我需要问题的查找匹配案例部分,感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你可以这样做

 // Your query string 
 $main_query = "SELECT 
                  COUNT(t1.id) as total, 
                  COUNT(t1.id) as 'total_$lang', 
                  CASE t2.`precence`
                    WHEN '0' THEN 'Absent'
                    WHEN '1' THEN 'Present'
                    ELSE 'Unknown'
                 END AS series,
                 CONCAT(DAYNAME(t1.date),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS ticks,
                 CONCAT(DATE_FORMAT(t1.date, '%a'),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS abv,
                 courses.no AS label,
                 CASE t2.`precence`
                   WHEN '0' THEN 'absent_$lang'
                   WHEN '1' THEN 'present_$lang'
                   ELSE 'unknown_$lang'
                 END AS 'attendance_state_$lang',
                 CONCAT(DAYNAME(t1.date),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS 'attendance_date_$lang',
                 CONCAT(DATE_FORMAT(t1.date, '%a'),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS 'attendance_date_abv_$lang',
                 courses.no AS 'course_num_$lang'

            FROM course_attendances as t1 left join `student_attendances` as t2 on t2.`course_attendance_id` = t1.`id`
            RIGHT JOIN courses ON courses.id = t1.course_id
            WHERE t1.`course_id` = '1' AND t1.date BETWEEN '2015-01-01' AND '2015-11-02'
            GROUP BY t1.`date`,t2.`precence`";

您的数据样本数组

$reportConfig = Array
     (
       'total_en' => 'Total',
       'absent_en' => 'Absent',
       'present_en' => 'Present',
       'unknown_en' => 'Unknown',
       'attendance_state_en' => 'Attendance State',
       'attendance_date_en' => 'Attendance Date',
       'attendance_date_abv_en' => 'Attendance Date (abv)',
       'course_num_en' => 'Course Number'
   );


 if(preg_match('/_\$lang/',$main_query)){
    foreach($reportConfig as $rep_conf_key=>$rep_conf_val){
       $rep_conf_key_temp = preg_replace('/_'.$lang.'/','_\\\$lang',$rep_conf_key);
       if(preg_match('/'.$rep_conf_key_temp.'/',$main_query)){
          $main_query = preg_replace('/'.$rep_conf_key_temp.'/',$rep_conf_val,$main_query);
       }
    }

 }
 echo '<pre/>'; print_r($main_query);

结果

SELECT 
COUNT(t1.id) as total, 
COUNT(t1.id) as 'Total', 
CASE t2.`precence`
        WHEN '0' THEN 'Absent'
        WHEN '1' THEN 'Present'
        ELSE 'Unknown'
    END AS series,
CONCAT(DAYNAME(t1.date),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS ticks,
CONCAT(DATE_FORMAT(t1.date, '%a'),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS abv,
    courses.no AS label,
CASE t2.`precence`
        WHEN '0' THEN 'Absent'
        WHEN '1' THEN 'Present'
        ELSE 'Unknown'
    END AS 'Attendance State',
CONCAT(DAYNAME(t1.date),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS 'Attendance Date',
CONCAT(DATE_FORMAT(t1.date, '%a'),' ',DATE_FORMAT(t1.date,'%Y %M %d')) AS 'Attendance Date (abv)',
courses.no AS 'Course Number'

 FROM course_attendances as t1 left join `student_attendances` as t2 on t2.`course_attendance_id` = t1.`id`
 RIGHT JOIN courses ON courses.id = t1.course_id
 WHERE t1.`course_id` = '1' AND t1.date BETWEEN '2015-01-01' AND '2016-11-17'
 GROUP BY t1.`date`,t2.`precence`