PHP Common How to Solutions

Loading

In this blog section, i will guide you through common how to in PHP! let begin our journey with examples.

  1. How to replace the part of string with another string in php
  2. How to count substrings occurs in a string in php
  3. How to extract substring from a string in PHP
  4. How to Check If a String Contains a Specific Word in PHP
  5. How to count total words count in PHP

How to replace the part of a string with another string in PHP?

here we will guide you how to replace the part of a string with another string in PHP. For achieving this you can this php function str_replace to find and replace and portion of string with another string, In the following example of Best Web Development Company of the string $my_str is replaced by the string “Best Web Development Company Name”. Lets try this now and see how it works:

<?php
$my_str = "Appfinz Technologies";
 
// Display replaced string
echo str_replace("Best Website Designing", "Best Web Development Company", $my_str);
?>

How to count substrings occurs in a string in php?

You can use the PHP substr_count() function to find out how many times a given substring is appears or repeated inside a string.

<?php
$quotes = "If we are talking about website development, Appfinz technologies provides website development services in delhi, this is best website development company in delhi, if you want to make website then connect with us for website development";
 
echo substr_count($quotes, 'website'); // Outputs: 5
echo substr_count($quotes, 'website development'); // Outputs: 4
?>

How to extract substring from a string in PHP?

Using the PHP common function substr() we can achieve this, The PHP substr() is used to get the substring Therefore the part of a part of string from string can be extracted using this function. This function takes the start and length parameters to return the portion of string.

<?php
$str = "Hello World!";
echo substr($str, 0, 5);  // Outputs: Hello
echo substr($str, 0, -7); // Outputs: Hello
echo substr($str, 0);     // Outputs: Hello World!
echo substr($str, -6, 5); // Outputs: World
echo substr($str, -6);    // Outputs: World!
echo substr($str, -12);   // Outputs: Hello World!
?>

How to Check If a String Contains a Specific Word in PHP?

we can use PHP strpos() function to check the specific words contains in another string or not.

The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false. Also note that string positions start at 0, and not 1.

Lets see this with a example:

<?php
$word = "website";
$mystring = "Appfinz Technologies is best website development company in india";
 
// Test if string contains the word 
if(strpos($mystring, $word) !== false){
    echo "Word Found!";
} else{
    echo "Word Not Found!";
}
?>

How to count total words count in PHP?

We can do this by using php function called str_word_count(), Let see how we count total words in php. this function help you to count words very effectively.

Note: You can also mix this function with javascript for real time words counts in textarea feilds.

Let see with a example

<?php
$my_str = 'Appfinz Technologies owned by kishan kumar mishra';
 
// Outputs: 7
echo str_word_count($my_str);
?>

About Post Author