fish man
fish man

Reputation: 2700

how to limit search box words in php

php How to limit words in a search box? I want limit words within 30 words and do not broken the phrase.

I think it should combine strlen and explode, but how to? and how to notice the custom when he is typing words?

Thanks.

Upvotes: 1

Views: 308

Answers (3)

Karolis
Karolis

Reputation: 9562

$string = preg_replace('/^((?:\s*\w+){0,30}).*$/s', '$1', $string);

Upvotes: 1

Eray
Eray

Reputation: 7128

You can check PHP's str_word_count function . You can count words .

Upvotes: 3

dynamic
dynamic

Reputation: 48091

That's javascript related not PHP.

You may want to use jQuery and attach an onChange event.

If you need the PHP script it's:

echo implode( ' ', array_slice(explode(' ',$search),0,30) ); //> Expensive

Upvotes: 4

Related Questions