Elton Jamie
Elton Jamie

Reputation: 584

How find words and strip the words after it?

$string = 'hello world php is a bla bla..';

How to keep 'hello world php', by finding if the string contains any 'is', and then remove all the characters after it?

I can't use substr because the occurrence of 'is' is not consistent, also it may be repeatable. How to catch the first one?

Upvotes: 0

Views: 98

Answers (2)

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

You can use explode:

<?php

$string = 'hello world php is a bla bla..';

$string = explode(' is ', $string);

echo $string[0];

?>

Read more at:

http://php.net/manual/en/function.explode.php

Upvotes: 1

Ayyanar G
Ayyanar G

Reputation: 1545

try like this,

$string = 'hello world php is a bla bla..';
    $str= substr($string, 0, strpos($string, "is"));

also you can use preg_replace,

$str = preg_replace('/ is.*/', '', $string );

Upvotes: 1

Related Questions