Showing posts with label php string. Show all posts
Showing posts with label php string. Show all posts

Tuesday, November 7, 2017

phpMyPassion

How To Reverse A String In PHP ?

Commonly in interview we have been asked about reverse a string. So here I am going to share the solution for this type of question with my article.

Image source : google images

This is a basic question but most of the developers are stuck in it due to hesitation of interview. Interviewer only want to ask the main logic to reverse a string. He can either ask the function in php to reverse a string or also ask about procedural logic behind this.

You can reverse a string using strrev() string function.

Syntax:                 

string strrev(string $string)

Output:

Return a string in reversed order.


So here are the commonly asked question with an example related to this.

#Reverse a string using PHP string function.

<?php
      echo strrev("php is my passion");  //output:- noissap ym si php
  ?>


#Reverse a specific word in a string.


<?php
        $string = "php is my passion";
        $array = explode(" ", $string);
        echo strrev($array[3]);                      //outpur:- noissap
   ?>

#Reverse a string without using string function.

<?php
       $s = 'php is my passion';
        $length = strlen($s); 
        for ($i = 0, $j = $length-1; $i < ($length / 2); $i++, $j--) {
            $t = $s[$i];
            $s[$i] = $s[$j];
            $s[$j] = $t;
        }
        echo $s;                           //Output:- noissap ym si php
   ?>

So using the above process you can reverse any type of string in PHP.

For More Check Video:-


Read More

Tuesday, March 21, 2017

anil

how to search a specific word in a string in php?

You can use the strpos function which is used to find the occurrence of one string inside other



strpos() :

strpos() function — Find the position of the first occurrence of a substring in a different string It's support (PHP 4, PHP 5, PHP 7).   



 Syntax :

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Find the numeric position of the first occurrence of needle in the haystack string.

Parameters :






haystack 
 
The string to search in.
 






needle 
 
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
 
 
offset 
 
If specified, search will start this number of characters counted from the beginning of the string. If the offset is negative, the search will start this number of characters counted from the end of the string.  




Output :

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
Returns FALSE if the needle was not found.





Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Examples :

Example #1 Using !==

<?php $a = 'How are you all?';

if (strpos($a, 'all') !== false) {
    echo 'true';
} ?>

Example #2 Using an offset


<?php// We can search for the character, ignoring anything before the offset$newstring = 'abcdef abcdef';$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0?>

 

source: php.net

 

Read More

Tuesday, January 24, 2017

anil

How to count the number of substring occurrences in a string ?


Common PHP issue that mostly developer face. below is the solution for such type of issue..


substr_count

substr_count — Count the number of substring occurrences

int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )

substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive.
Note:
This function doesn't count overlapped substrings. See the example below!

Parameters 

haystack
The string to search in
needle
The substring to search for
offset
The offset where to start counting. If the offset is negative, counting starts from the end of the string.
length
The maximum length after the specified offset to search for the substring. It outputs a warning if the offset plus the length is greater than the haystack length. A negative length counts from the end of haystack.

Return Values 

This function returns an integer.
source: php.net
<?php $str = 'abc.phpmypassion.com';
echo strlen($str); // 21echo substr_count($str, '.'); //
// the string is reduced to 'phpmyps', so it prints 1
echo substr_count($str, '.', 4);//the text is reduced to 'phpm',so it prints 0
echo substr_count($str, '.', 4, 4);// generates a warning because 10+12 > 21
echo substr_count($str, '.', 10, 12);// prints only 3, because it doesn't count overlapped substrings
$str2= 'phpmypassion.com'; echo substr_count($str2, 'p');?>

Check Video :- 

 

Read More

Wednesday, January 18, 2017

anil

How to remove everything before a specific first character?


Sometime developers are facing the issue of eliminating all characters before the first specific character. Here I am describing how you can remove all the characters before a specific thing.

Just suppose we have a string as below:-


<?php
    $str = 'http://blog.phpmypassion.com'; 
?>

Now If I want to get my main domain then I have to follow below code..

<?php
if(($pos = strpos($str, '.')) !== false)
{
   $new_str = substr($str, $pos + 1);
}
else
{
   $new_str = get_last_word($str);
}
echo 'new string will be='.$new_str;
?>

Output:-

So the output will be "new string will be=phpmypassion.com"

It is a easiest way to remove everything before a specific characters position.

Check Video:-


Read More