如何在 PHP 中编写正则表达式来获取标题


How can I write a regex in PHP to grab titles?

我想写一个正则表达式来获取书籍/电影标题。

到目前为止,我用PHP写了这个:

(?:                                # Start of group:
'b                                # Match start of a word
(?:                               # Start of inner group:
[A-Z]*
[A-Z][a-z]*                      # Either match an uppercase word
|                                 # or
(?:a[nts]|the|by|for|i[nt]|      # one of these "special" words
 o[fnr]|to|up|and|but|nor)
)                                 # End of inner group
'b                                # Match end of word
's*                              # Match one or more whitespace characters
)+                                 # Match one or more of the above.

我的意见如下:

I watched the movie The Girl With the Dragon Tattoo but it wasn't very good.

这匹配:

I
the
The Girl With the Dragon Tattoo but it

我知道这是一个复杂的问题,虽然我希望它只返回:

The Girl With the Dragon Tattoo

我会同意:

I
The Girl With the Dragon Tattoo

我如何更改我的正则表达式来实现此目的?

据我了解,您希望匹配任何用户输入并查找书名或电影名称。

如果你有一个非常好的书籍/电影数据库,你可以做的就是创建一个算法。

例如,始终将输入设置为小写,并检查数据库中的每个标题。

如果您设法找到匹配项:您可以在标题前后匹配几个单词。您可以将它们保存到数据库。之后,当您检查输入但找不到标题时,您可以根据以前的输入创建preg_match并确定最接近标题的输入。

如果幸运的话,您可以将新标题保存到数据库中。

我不认为这会稍微接近好。