用php获取文本部分的方法

时间:2015-05-07 16:56:46

标签: php regex preg-replace preg-match

您好我搜索一个简单的方法来获取文本的部分内容:

Playtech Ltd., London (Teststreet 3, 1234 London, Sale all interesting things.). Directors: Paul, Test, London, *01.01.1900; Lisa, Test, London, *01.01.1901.

我想要这个:

firm: Playtech Ltd.
street: Teststreet 3
postcode: 1234
city: London
sale: Sale all interesting things.
directors: Paul, Test, London, *01.01.1900; Lisa, Test, London, *01.01.1901

有时还有其他格式:

例如:

ABC 12345:Playtech Ltd., London (Teststreet 3, 1234 London). A Firm. Sale: Sale all interesting things. Directors: Paul, Test, London, *01.01.1900; Lisa, Test, London, *01.01.1901.

是否有可能使用php制作它?因为我必须为5.000文本做这件事。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容进行匹配:

^(?:.*?:)?(.*?),\s*.*?\((.*?),\s*(\d+)\s*(.*?),\s*(.*?)\)\.\s*.*?(?:(?:Board of )?Directors):\s*(.*)$

并替换为/ extract out:

firm: $1\nstreet: $2\npostcode: $3\ncity: $4\nsale: $5\ndirectors: \6\n

<强>输出:

firm: Playtech Ltd.
street: Teststreet 3
postcode: 1234
sale: Sale all interesting things.
directors: Paul, Test, London, *01.01.1900; Lisa, Test, London, *01.01.1901.

请参阅updated DEMO