<?php $str = '主要有以下几个文件:index.php, style.css, common.js'; //将目标字符串$str中的文件名替换后增加em标签 $pattern = '/(\w+\.\w+), (\w+\.\w+), (\w+\.\w+)/i'; //preg_match($pattern,$str,$matches); //print_r($matches); $replacement = '<em>$1</em>, <em>$2</em>, <em>$3</em>'; echo preg_replace($pattern,$replacement,$str);