من از این استفاده می کنم :
function after($this, $inthat) {
if(!is_bool(strpos($inthat, $this))) {
return substr($inthat, strpos($inthat,$this)+strlen($this));
}else{
return false;
}
}
function before($this, $inthat) {
return substr($inthat, 0, strpos($inthat, $this));
}
function between($this, $that, $inthat) {
return before($that, after($this, $inthat));
}
function occurrencecount($this, $that, $inthat, $count) {
$newinthat = after("$that", after("$this", $inthat));
if($newinthat != false){
$count = $count + 1;
return occurrencecount($this, $that, $newinthat, $count);
}else{
return $count;
}
}
$xml = 'Line 222: <Tag>phrase1</Tag>Line 241: <Tag>phrase2</Tag>Line 260: <Tag>phrase3</Tag>Line 279: <Tag>phrase4</Tag>Line 298: <Tag>phrase5</Tag>Line 317: <Tag>phrase6</Tag>';
while (occurrencecount('<Tag>','</Tag>',$xml,'0')){
$ex = between ('<Tag>','</Tag>',$xml); $xml = str_replace ('<Tag>'.$ex.'</Tag>','',$xml); $ex .= '\n\r'.$ex; }
echo $ex;