Belgin Fish
Belgin Fish

Reputation: 813

Remove All Text After Certain Point

Hey, I'm just wondering how I could remove all the text if it encounters a certain string inside a string :

EX: 24 Season 1 Episode 3

I would like, that if it find the text Season that it removes it and everything after so it would just leave you with :

24

Thanks

Ah sorry I forgot to say I need this in PHP.

Upvotes: 4

Views: 10867

Answers (4)

anujayk
anujayk

Reputation: 584

1.If u need all the strings which occurs after certain word this will work

$str="24 Season 1 Episode 3"; echo strstr($str, 'Season');

2.If u need all the strings which occurs before certain word this will work

$str="24 Season 1 Episode 3";
$arr = explode("Season", $str, 2);
$first = $arr[0];
echo $first;

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342659

$string="24 Season 1 Episode 3";
$s = explode("Season",$string,2);
echo $s[0];

Upvotes: 1

Gordon
Gordon

Reputation: 317119

With PHP5.3. you can use

echo strstr('24 Season 1 Episode 3', 'Season', true); // outputs 24

See

and the comments on that page to see workarounds for PHP < 5.3

Upvotes: 9

fastcodejava
fastcodejava

Reputation: 41117

Can use String.replaceAll("Season.*", "");

Upvotes: 0

Related Questions