Allen Gingrich
Allen Gingrich

Reputation: 5657

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>

Upvotes: 1

Views: 78

Answers (2)

Sepero
Sepero

Reputation: 4677

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

Upvotes: 1

David Chan
David Chan

Reputation: 7505

try this, the .* means any number of any character

/^Find.*eBay\.$/

Upvotes: 4

Related Questions