Reputation: 10061
I want first 3 words from a non-english string in php. For example:
এখানে কিংকর্তব্যবিমূঢ় হবার কোনো সুযোগ নেই
I have found it is possible for English text as:
$phrase = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
echo implode(' ', array_slice(str_word_count($phrase, 2), 0, 3));
But it does not work for my non-english(bengali) text. Could someone tell me how can I do it?
Upvotes: 1
Views: 79
Reputation: 12085
use explode
and implode
1) explode
the string by space
2) get the first three slice using array_slice
3) again implode
it with space
<?php
$string = 'এখানে কিংকর্তব্যবিমূঢ় হবার কোনো সুযোগ নেই';
$array = array_slice(explode(" ",$string),0,3);
echo implode(" ",$array)."<br>";
?>
Upvotes: 1
Reputation: 43199
Here as well, you could use the power of regular expressions:
^(?:\b\p{Bengali}+\s*){3}
PHP
would be:
<?php
$string = "এখানে কিংকর্তব্যবিমূঢ় হবার কোনো সুযোগ নেই";
$regex = '~^(?:\b\p{Bengali}+\s*){3}~u';
if (preg_match($regex, $string, $match)) {
echo $match[0];
}
# এখানে কিংকর্তব্যবিমূঢ় হবার
?>
See a demo on ideone.com.
Upvotes: 0
Reputation: 75635
You do not mention what version of PHP you are using but the main pain with PHP and UTF is that it does not sometimes work together as many string manipulation methods are/were mostly based on assumption "1 char = 1 byte". MBString extension addresses this in many cases yet not everywhere. In your case str_word_count()
needs to be replaced with multibyte aware counterpart, i.e. mb_split()
:
echo implode(' ', array_slice(mb_split("\s", $phrase), 0, 3));
Upvotes: 1
Reputation: 99041
You can use explode
, array_slice
and implode
for this:
$string = "এখানে কিংকর্তব্যবিমূঢ় হবার কোনো সুযোগ নেই";
$parts = explode(" ", $string);
$x = array_slice($parts, 0, 3);
print implode(" ", $x);
# এখানে কিংকর্তব্যবিমূঢ় হবার
Upvotes: 1