support@codebucket.net

How to Add or Remove characters from a string

How to Add or Remove characters from a string

K. M. Shawkat Zamil | October 11, 2022

Hello Developers,

In this demonstration, you will find the usage of substr() in PHP. 

 

The substr() function requires three parameters. These are given below:

 

substr(string $string, int $offset, ?int $length = null): string

 

Returns the portion of string specified by the offset and length parameters. First, we have to open up a code editor and paste the below code to run the file to see the output. We can see two examples from the below code:

 

<?php

$name="John Doe";

//if length is negative it starts removing from the end
$result = substr($name,0,-3);
echo $result; echo '<br>';

//if length is positive it starts removing from the begining
$result = substr($name, 0, 6);
echo $result;

 

Output:

 

John
John r

 

I hope this might help you in the journey of development.

 

Read More: How to read multiple query stored procedure from mssql to Laravel project

 

K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.