Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Removing startup words and ending words regular expression

I have a string of this format and I want only the-big-bang-theory from this string.The problem is in this string some words are of variable length

media-tv-quotes-2-the-big-bang-theory-94-toprated-2
       ^                               ^    ^     ^
----------------------------------------------------

I try with this code but no luck.

$keywords = str_replace("/media-[a-z]-quotes-+/","", "media-tv-quotes-2-the-big-bang-theory-94-toprated-2");

Anybody knows how can I achieve this.

Thanks..

Upvotes: 0

Views: 65

Answers (3)

Glavić
Glavić

Reputation: 43572

Fetch anything between 2 numbers:

$s = 'media-tv-quotes-2-the-big-bang-theory-94-toprated-2';
preg_match('~\d+-(.+?)-\d+~', $s, $matches);
print_r($matches);

Problem acuress if your movie title has numbers in it, in that case use:

$s = 'media-tv-quotes-2-the-big-bang-theory-94-toprated-2';
preg_match('~^media-\w+-\w+-\d+-(.+?)-\d+-\w+-\d+$~i', $s, $matches);
print_r($matches);

Upvotes: 1

hsz
hsz

Reputation: 152266

Try with following regex:

$keywords = preg_replace("/^media-[a-z]+-quotes-\d+-(.*?)-\d+-.*$/", "$1", "media-tv-quotes-2-the-big-bang-theory-94-toprated-2");

Upvotes: 1

Stephan
Stephan

Reputation: 43053

Try this :

$keywords = preg_replace("/^(.*?)(the-big-bang-theory)(.*)$/i","$2", "media-tv-quotes-2-the-big-bang-theory-94-toprated-2");

Capture anything characters before the-big-bang-theory and after it. Then only preserve in the replacement the-big-bang-theory.

Upvotes: 0

Related Questions