按名字和姓氏分割名稱。
$split = explode(' ', $name);
$first = $split[0];
$last = $split[1];
preg_replace(
"/(\(?($first)\)? \(?($last)\))/"
, $replacement
, $text
);
更動態的方法
// split name string into sub-names
$split = explode(' ', $name);
// initiate the search string
$search = '';
// loop thru each name
// solves the multiple last or middle name problem
foreach ($split as $name) {
// build the search regexp for each name
$search .= " \(?$name\)?";
}
// remove first space character
$search = substr($search, 1);
// preg_replace() returns the string after its replaced
// note: $replacement isn't defined, left it for you :)
// note: the replacement will be lost if you don't
// print/echo/return/assign this statement.
preg_replace(
"/($search)/"
, $replacement
, $text
);
被括號中的唯一可能的變化? – 2014-02-11 18:49:33
您可以使用正則表達式,但首先您必須定義確切的要求,括號的類型等。 – jeroen
@圓括號是唯一可能的變體。 –