这个问题是我之前问题的延续:
我有这样的文字:
<ORGANIZATION>Head of Pekalongan Regency</ORGANIZATION>, Dra. Hj.. Siti Qomariyah , MA and her staff were greeted by <ORGANIZATION>Rector of IPB</ORGANIZATION> Prof. Dr. Ir. H. Herry Suhardiyanto , M.Sc. and <ORGANIZATION>officials of IPB</ORGANIZATION> in the guest room.
使用我之前问题的答案代码并PREG_OFFSET_CAPTURE
添加如下:
function get_text_between_tags($string, $tagname) {
$pattern = "/<$tagname\b[^>]*>(.*?)<\/$tagname>/is";
preg_match_all($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
if(!empty($matches[1]))
return $matches[1];
return array();
}
我得到一个输出:
Array (
[0] => Array ( [0] => 北加浪岸县长 [1] => 14 )
[1] => Array ( [0] => IPB 校长 [1] => 131 )
[2] => 数组 ( [0] => IPB 官员 [1] => 222 ) )
14、131、222是匹配模式时的字符索引。我可以得到单词的索引吗?我的意思是这样的输出:
Array (
[0] => Array ( [0] => 北加浪岸县长 [1] => 0 )
[1] => Array ( [0] => IPB 校长 [1] => 15)
[2] => 数组 ( [0] => IPB 官员 [1] => 27 ) )
PREG_OFFSET_CAPTURE
除了或需要更多代码之外,还有其他方法吗?我不知道。感谢帮助。:)