[英]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,並確定最接近標題的位置。
如果幸運的話,可以將新標題保存到數據庫中。
我認為這不太可能達到效果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.