1

我在下面有一些这样的文本,我想要删除最后一个地址匹配,问题是这些地址有不同的格式,取决于文本:

我有这个:

To find out more about Groupon Goods, see the <a href="http://www.groupon.co.uk/faq"> FAQ</a>.\n\nUK-Bedding Returns:Albany Mill, Old Hall Street, Middleton, Manchester M241AG UK-Bedding Returns:Albany Mill, Old Hall Street, Middleton Manchester M241AG\n\n<a href="http://www.groupon.co.uk/terms_and_conditions"></a> Built by gskinner.com with Fl

输出应该是这样的(我删除了最后一个“Albany Mill, Old Hall Street, Middleton Manchester M241AG”):

To find out more about Groupon Goods, see the <a href="http://www.groupon.co.uk/faq"> FAQ</a>.\n\nUK-Bedding Returns:Albany Mill, Old Hall Street, Middleton, Manchester M241AG UK-Bedding Returns:\n\n<a href="http://www.groupon.co.uk/terms_and_conditions"></a> Built by gskinner.com with Fl

4

1 回答 1

1

如果总是有重复,我会采取不同的方式:

  • 用换行符分割文本(使用 preg_split 因为有多个换行符);
  • 找到地址所在的行;
  • 把它切成两半。

因此,在您的示例中,类似于(额外详细):

$lines = preg_split('#\n+#', $text);
$addresses = $lines[1];
$address = substr($addresses, 0, floor(strlen($addresses) / 2));
于 2013-01-03T12:40:33.600 回答