简体   繁体   中英

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

I would like to find and return a sentence that starts with "Find" and ends with "eBay.", but I cannot get it to work. Here's what I have now:

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));

                }

Any advice would be great. Thanks!

Edit**

This is the string I'm searching:

<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\.$/

Your example string doesn't start with "Find" nor end with "eBay", so it will not match your example string.

To match your example, you will need to use something like this:

/eBay\<\/a\>Find/

That will match a string that contains eBay</a>Find (though you could use just use a simple search for that instead of a regex).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM