我想能夠提取字符串的某些部分並返回唯一數組。這裏是我的字符串:使用Php preg_match多次發生,返回唯一數組
$string = "
<div> some text goes here... **css/method|1|2**</div>
<div>**php/method|3|4**</div>
<div>**html|method|6|9** and more text here</div>
<div>**html/method|2|5**</div>
";
preg_match_all()
$pattern = "/**(.*?)**/";
preg_match_all($pattern, $string, $matches);
我可以提取所有字符串中的部分,但我需要進一步走一步,只返回以下內容:
CSS ,PHP和HTML。
最終陣列應該是這樣的:
$result = array("css", "php", "html");
所以基本上,我需要在這種情況下,「HTML」,以消除重複值,以及提取反斜槓或管之前的每個值。我不關心方法部分以及後續部分。
'*'在正則表達式的特殊含義,你需要躲避他們的。 – Barmar