Horje

Tips (Total 15)


# Tips-1) How to check if a string contain multiple specific words

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()

If match some word in php

index.php
Example: PHP
<?php
$data = "bangla";
if (strpos($data, 'bangla')!==false || strpos($data, 'english')!== false) {
    echo "Found";
} else {
    echo "Not Found";
}
?>

Output should be:

If match some word in php

PHP Match Specific Word

Here is a function that can perform this operation without using regular expressions which could be slower. Instead of passing a single string for the task, pass an array like
index.php
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';
}

?>

Output should be:

PHP Match Specific Word

If Match Some Word

For this, you will need Regular Expressions and the preg_match function. Something like:

index.php
Example: PHP
<?php
$data = 'bad';
if(preg_match('(bad|naughty)', $data) === 1) 
{
echo 'Yes';    
} else {
echo 'No';    
}
?>

Output should be:

If Match Some Word

If Multiple Word Match

(preg_match return 1 if there is a match in the string, 0 otherwise).

multiple str_pos calls

index.php
Example: PHP
<?php
$data = 'bad';
if (strpos($data, 'bad')!==false or strpos($data, 'naughty')!== false) {
    echo "Found";
} else {
    echo "Not Found";
}
?>

Output should be:

If Multiple Word Match

# Tips-2) How to extract words from Sentence in PHP

Extract Words like keywords from Sentence by PHP

PHP Function for extracting Words

Just use it in functional. Create a functions.php file and Insert following codes
functions.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;
}
?>

Here you can result out of texts from sentence

Just Copy and Paste following code to index.php Make a index.php file

index.php
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> ';
} 
?>

Output should be:

Here you can result out of texts from sentence

# Tips-3) How to check if string is_numeric() Function in PHP

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 )

How to add is_numeric() Function in PHP

Follow the example.

index.php
Example: PHP
<?php
 
$num = 12;
if (is_numeric($num)) {
        echo $num . " is numeric";
    }
    else {
        echo $num . " is not numeric";
    }
 
?>

Output should be:

How to add is_numeric() Function in PHP

# Tips-4) How to get only text from HTML Source Code by PHP

Here is tag strip_tags

How to extract only text from HTML Source Code by PHP

Try the Example.
index.php
Example: PHP
<?php
$your_string = 'This html Code <html>';
echo(strip_tags($your_string));
?>

Output should be:

How to extract only text from HTML Source Code by PHP

# Tips-5) How to get first 3 words from a sentence by PHP

The tag is: array_slice

How to extract first 3 words from a sentence by PHP

Follow the Example.
index.php
Example: PHP
<?php
$first3word = 'WWE is the best sports'; 
$first3word = implode(' ', array_slice(explode(' ', $first3word), 0, 3));
echo $first3word;
?>

Output should be:

How to extract first 3 words from a sentence by PHP

# Tips-6) How to work if empty value does not else

Sometimg If you use if empty tag for php it does not work else element.

Use if (!empty(trim($dimensions)))

How to use empty value if else not working

Follow the example.

index.php
Example: PHP
<?php if (!empty(trim($dimensions))) { echo $dimensions; } else { echo 'No';  } ?>
NO
How to use empty value if else not working

# Tips-7) How to remove first Five Line from a TXT File in PHP

Here made a way to delete first 5 lines from a text file.

How to extract first Five Line from a TXT File in PHP

Follow the Example.
index.php
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));

?>

# Tips-8) How to get Longest Word from a Sentence in PHP

Here is simple Exampple to get Longest Word from a Sentence in PHP.

How to extract Longest Word from a Sentence in PHP

Follow the Example
index.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"
?>

# Tips-9) How to remove first character from string php

The substr() function will probably help you here:

How to delete first character from string php

See below Example.
index.php
Example: PHP
<?php
$str = 'We love Cricket';
$str = substr($str, 1);
echo $str ;
?>

# Tips-10) How to count if Number is below than 10 sum

AFAIK You can´t do this with your aggregation query. Launch the query and, by code, check the count.

How to count if string is below than 10 sum

Follow the Example.

index.php
Example: PHP
<?php
$data = 5;
 if ( $data < 10 ){
    echo 'Below';
 } else {
    echo 'Upper';
 }
?>

# Tips-11) How to get back page link

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.

How to extract back page link in PHP

Follow the Example.
index.php
Example: PHP
<?php
echo '<a href="'.$_SERVER['HTTP_REFERER'].'">Go back</a>';
?>


# Tips-12) How to get first 3 longest words from a sentence PHP

The solution would be like this:
  • 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:

Method - 1

See the Example.

index.php
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 />';
}
?>

Output should be:

Method - 2

Alternative method: (Using predefined functions)

index.php
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);
?>

Output should be:

Method - 3

See my Example.

index.php
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;
?>

Output should be:

Method - 4

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]);
}
?>

Output should be:


# Tips-13) How to count number of words from sentence in PHP

It will count number of Words from a String.

How to count Words in PHP

See the Example.
index.php
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; 
?>

Output should be:

How to count Words in PHP

# Tips-14) How to remove all number from a string php

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.
  • The modifier /u enables unicode string treatment. This modifier is important, otherwise the numerals would not match.

How to remove all numbers from string? PHP

I'd like to remove all numbers from a string [0-9]. I wrote this code that is working.
index.php
Example: PHP
<?php
$words = '१३३७ 11544 I love';
$words = preg_replace('/\d+/u', '', $words);
var_dump($words);
?>

Output should be:

How to remove all numbers from string? PHP

# Tips-15) How to create first word Upper/Big from a sentence

Here is PHP Function to make frist word upper from a Sentence.

Create first word Big from a Sentence PHP

See our Example.

index.php
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);
?>

Output should be: