簡體   English   中英

如何使用PHP正則表達式查找並返回字符串的一部分?

[英]How to use PHP Regular Expressions to find and return a portion of a string?

我想查找並返回一個以“ Find”開頭並以“ eBay。”結尾的句子,但是我無法使其正常工作。 這是我現在所擁有的:

if (preg_match("/^Find eBay\.$/", $post->post_content) == 1) {

                    $description = preg_grep("/^Find eBay\.$/", $post->post_content);

                } else {

                    $description = $this->trim_excerpt_without_filters($this->internationalize($post->post_content));

                }

任何建議都很好。 謝謝!

編輯**

這是我正在搜索的字符串:

<p><a href="http://cgi.ebay.com/ebaymotors/?cmd=ViewItem&amp;_trksid=p3984.m1438.l2649&amp;item=110804005978&amp;sspagename=STRK%3AMEWAX%3AIT">here on eBay</a>Find this 1969 Chevrolet Camaro COPO 427 for sale in New York, .</p>

試試這個,。*表示任意數量的任何字符

/^Find.*eBay\.$/

您的示例字符串不是以“查找”開頭,也不是以“ eBay”結尾,因此它與您的示例字符串不匹配。

為了匹配您的示例,您將需要使用以下內容:

/eBay\<\/a\>Find/

它將匹配包含eBay</a>Find的字符串(盡管您可以使用簡單的搜索來代替regex)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM