- Use
explode()
to get the words of the string in an array - "Partially" sort the array elements in descending order of length
- Use a simple
for
loop to print the longest three words from the array.
So your code should be like this:
substr_count()
is counting for how many times the string appears, so when it's 0 then you know that it was not found. I would say this way is more readable than using str_pos()
Example:
PHP
<?php
$data = "bangla";
if (strpos($data, 'bangla')!==false || strpos($data, 'english')!== false) {
echo "Found";
} else {
echo "Not Found";
}
?>
Example:
PHP
<?php
// function
function strposMultiple($haystack, $needle, $offset = 0) {
if(is_string($needle))
return strpos($haystack, $needle, $offset);
else {
$min = false;
foreach($needle as $n) {
$pos = strpos($haystack, $n, $offset);
if($min === false || $pos < $min) {
$min = $pos;
}
}
return $min;
}
}
// PHP Code for Result
$data = 'bad';
if (strposMultiple($data, ['bad', 'naughty']) !== false) {
echo 'Matched';
} else {
echo 'Not Matched';
}
?>
For this, you will need Regular Expressions and the preg_match function. Something like:
Example:
PHP
<?php
$data = 'bad';
if(preg_match('(bad|naughty)', $data) === 1)
{
echo 'Yes';
} else {
echo 'No';
}
?>
(preg_match return 1 if there is a match in the string, 0 otherwise).
Example:
PHP
<?php
$data = 'bad';
if (strpos($data, 'bad')!==false or strpos($data, 'naughty')!== false) {
echo "Found";
} else {
echo "Not Found";
}
?>
Extract Words like keywords from Sentence by PHP
Example:
PHP
<?php
// Extract Tag
function extractCommonWords($string){
$stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
$string = preg_replace('/\s\s+/i', '', $string); // replace whitespace
$string = trim($string); // trim the string
$string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
$string = strtolower($string); // make it lowercase
preg_match_all('/\b.*?\b/i', $string, $matchWords);
$matchWords = $matchWords[0];
foreach ( $matchWords as $key=>$item ) {
if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 5 ) {
unset($matchWords[$key]);
}
}
$wordCountArr = array();
if ( is_array($matchWords) ) {
foreach ( $matchWords as $key => $val ) {
$val = strtolower($val);
if ( isset($wordCountArr[$val]) ) {
$wordCountArr[$val]++;
} else {
$wordCountArr[$val] = 1;
}
}
}
arsort($wordCountArr);
$wordCountArr = array_slice($wordCountArr, 0, 10);
return $wordCountArr;
}
?>
Just Copy and Paste following code to index.php Make a index.php file
Example:
PHP
<?php
include("functions.php");
$text = 'Cricket is the Most Popular Game in the World';
$array = extractCommonWords($text);
echo 'Tag:';
foreach($array as $k => $k) {
echo '<span class="badger badger-darkener"><a href="'.$url.'/browse/'.strtolower($catss).''.strtolower($k).'" style="color:blue;">#'.$k.'</a></span> ';
}
?>
The is_numeric() function is an inbuilt function in PHP which is used to check whether a variable passed in function as a parameter is a number or a numeric string or not. The function returns a boolean value.
Syntax:
bool is_numeric ( $var )
Follow the example.
Example:
PHP
<?php
$num = 12;
if (is_numeric($num)) {
echo $num . " is numeric";
}
else {
echo $num . " is not numeric";
}
?>
Here is tag strip_tags
Example:
PHP
<?php
$your_string = 'This html Code <html>';
echo(strip_tags($your_string));
?>
The tag is: array_slice
Example:
PHP
<?php
$first3word = 'WWE is the best sports';
$first3word = implode(' ', array_slice(explode(' ', $first3word), 0, 3));
echo $first3word;
?>
Sometimg If you use if empty tag for php it does not work else element.
Use if (!empty(trim($dimensions)))
Follow the example.
Example:
PHP
<?php if (!empty(trim($dimensions))) { echo $dimensions; } else { echo 'No'; } ?>
Here made a way to delete first 5 lines from a text file.
Example:
PHP
<?php
$file_out = file("f.txt"); // Read the whole file into an array
//Delete the recorded line
unset($file_out[0]);
unset($file_out[1]);
unset($file_out[2]);
unset($file_out[3]);
unset($file_out[4]);
//Recorded in a file
file_put_contents("f.txt", implode("", $file_out));
?>
Here is simple Exampple to get Longest Word from a Sentence in PHP.
Example:
PHP
<?php
$string = "Where did the big Elephant go?";
$words = explode(' ', $string);
$longestWordLength = 0;
$longestWord = '';
foreach ($words as $word) {
if (strlen($word) > $longestWordLength) {
$longestWordLength = strlen($word);
$longestWord = $word;
}
}
echo $longestWord;
// Outputs: "Elephant"
?>
The substr()
function will probably help you here:
Example:
PHP
<?php
$str = 'We love Cricket';
$str = substr($str, 1);
echo $str ;
?>
AFAIK You can´t do this with your aggregation query. Launch the query and, by code, check the count.
Follow the Example.
Example:
PHP
<?php
$data = 5;
if ( $data < 10 ){
echo 'Below';
} else {
echo 'Upper';
}
?>
Here is a small example on how you can implement it:
When You click a Page Link, After going that page you will get past Link Page from where you clicked link.
Example:
PHP
<?php
echo '<a href="'.$_SERVER['HTTP_REFERER'].'">Go back</a>';
?>
explode()
to get the words of the string in an arrayfor
loop to print the longest three words from the array.So your code should be like this:
See the Example.
Example:
PHP
<?php
$text = 'one four eleven no upstairs';
$arr = explode(" ", $text);
$count = count($arr);
for($i=0; $i < $count; $i++){
$max = $arr[$i];
$index = $i;
for($j = $i + 1; $j < $count; ++$j){
if(strlen($arr[$j]) > strlen($max)){
$max = $arr[$j];
$index = $j;
}
}
$tmp = $arr[$index];
$arr[$index] = $arr[$i];
$arr[$i] = $tmp;
if($i == 3) break;
}
// print the longest three words
for($i=0; $i < 3; $i++){
echo $arr[$i] . '<br />';
}
?>
Alternative method: (Using predefined functions)
Example:
PHP
<?php
$text = 'one four eleven no upstairs';
$arr = explode(" ", $text);
usort($arr,function($a, $b){
return strlen($b)-strlen($a);
});
$longest_string_array = array_slice($arr, 0, 3);
// display $longest_string_array
var_dump($longest_string_array);
?>
See my Example.
Example:
PHP
<?php
$text = 'one four eleven no upstairs';
$arr = explode(" ", $text);
$count = count($arr);
for($i=0; $i < $count; $i++){
$max = $arr[$i];
$index = $i;
for($j = $i + 1; $j < $count; ++$j){
if(strlen($arr[$j]) > strlen($max)){
$max = $arr[$j];
$index = $j;
}
}
$tmp = $arr[$index];
$arr[$index] = $arr[$i];
$arr[$i] = $tmp;
if($i == 3) break;
}
// print the longest three words
for($i=0; $i < 3; $i++){
$long .= $arr[$i] . ' ';
}
echo $long;
?>
You need to create your own comparative function and pass it with array to usort php function. Ex.:
<?php
function lengthBaseSort($first, $second) {
return strlen($first) > strlen($second) ? -1 : 1;
}
$text = 'one four eleven no upstairs';
$arr = explode(" ", $text);
usort($arr, 'lengthBaseSort');
var_dump(array_slice($arr, 0, 3));
It will output 3 longest words from your statement.
According to author changes:
If you have no ability to use usort for some reasons (may be for school more useful a recursive function) use following code:
<?php
$text = 'one four eleven no upstairs';
$arr = explode(" ", $text);
function findLongest($inputArray) {
$currentIndex = 0;
$currentMax = $inputArray[$currentIndex];
foreach ($inputArray as $key => $value) {
if(strlen($value) > strlen($currentMax)){
$currentMax = $value;
$currentIndex = $key;
}
}
return [$currentIndex, $currentMax];
}
for($i = 0; $i < 3; $i++) {
$result = findLongest($arr);
unset($arr[$result[0]]);
var_dump($result[1]);
}
?>
It will count number of Words from a String.
Example:
PHP
<?php
// PHP program to count number of
// words in a string
$str = "I Love US";
// Using str_word_count() function to
// count number of words in a string
$len = str_word_count($str);
// Printing the result
echo $len;
?>
For Western Arabic numbers (0-9):
$words = preg_replace('/[0-9]+/', '', $words);
For all numerals including Western Arabic (e.g. Indian):
$words = '१३३७';
$words = preg_replace('/\d+/u', '', $words);
var_dump($words); // string(0) ""
\d+
matches multiple numerals./u
enables unicode string treatment. This modifier is important, otherwise the numerals would not match.Example:
PHP
<?php
$words = '१३३७ 11544 I love';
$words = preg_replace('/\d+/u', '', $words);
var_dump($words);
?>
Here is PHP Function to make frist word upper from a Sentence.
See our Example.
Example:
PHP
<?php
function uc_first_word ($string)
{
$s = explode(' ', $string);
$s[0] = strtoupper($s[0]);
$s = implode(' ', $s);
return $s;
}
$title = 'Wwe Raw 2025';
echo uc_first_word($title);
?>