Scott Worsnop
Scott Worsnop

Reputation: 3

Get substring before a specified substring

$title = 'the.test.hd.part12';

I need to keep the characters up until it finds the word "part".

Should return - the.test.hd.

Upvotes: 0

Views: 1583

Answers (3)

DKSan
DKSan

Reputation: 4197

For users on PHP Version < 5.3.0:

echo substr($title, 0, strpos($title, $needle));

Upvotes: 0

Vikk
Vikk

Reputation: 3373

echo current(explode('part12',$title));

Upvotes: 0

xdazz
xdazz

Reputation: 160833

echo strstr($title, 'part', true); // As of PHP 5.3.0

Upvotes: 2

Related Questions