我是新手。我正在嘗試在以下任一行中找到全名,并且沒有Obituary for
<h2>Obituary for John Doe</h2>
<h1>James Michael Lee</h1>
我的正則運算式是這樣的。
(<h1>(. ?)<\/h1>|<h2>Obituary\sfor\s(. ?)<\/h2>)
我得到的仍然是Obituary for John Doe
. 如何洗掉Obituary for
?
uj5u.com熱心網友回復:
許多條路通羅馬,你可能可以這樣做:
<h(?:1>|2>Obituary\sfor\s)\K[^><]
請參閱 regex101 中的此演示。比賽將在$out[0]
.
\K
重置報告匹配的開始。有關更多資訊,請參閱SO 正則運算式常見問題解答。
uj5u.com熱心網友回復:
你能在不使用正則運算式的情況下做這樣的事情嗎?
/**
* @description : Function extracts names from html header tags
* @example : "<h2>Obituary for John Doe</h2><h1>James Michael Lee</h1>" -> ["John Doe", "James Michael Lee"]
* @param $html string
* @return []string : list of full names
*/
function extractFullNames($html) {
$regex = '/<h[1-2]>(.*?)<\/h[1-2]>/';
preg_match_all($regex, $html, $matches);
$names = $matches[1];
$names = array_map('trim', $names);
$names = array_map('strip_tags', $names);
$names = array_map('strtolower', $names);
$names = array_map('ucwords', $names);
$names = array_map('removeObituary', $names);
return $names;
}
/**
* @description : Function used to remove "Obituary For" if present
* @example : "Obituary For John Doe" -> "John Doe"
* @param $name string
* @return string : name without "Obituary For"
*/
function removeObituary($name) {
$name = str_replace("Obituary For ", "", $name);
return $name;
}
// Test cases
$html = '<h2>Obituary for John Doe</h2><h1>James Michael Lee</h1>';
$names = extractFullNames($html);
$expected = ['John Doe', 'James Michael Lee'];
echo "Expected: " . implode(', ', $expected) . "\n";
echo "Actual: " . implode(', ', $names);
uj5u.com熱心網友回復:
我可能會做類似的事情
/^(?:\s<[^>]*?>)?(?:.*\s for\s )?([^<]*)/
并提取$1
(第一個匹配組)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/468963.html