Delete first 3 characters and last 3 characters from String PHP
Pass a negative value as the length
argument (the 3rd argument) to substr()
, like:
$result = substr($string, 3, -3);
So this:
<?php
$string = "Sean Bright";
$string = substr($string, 3, -3);
echo $string;
?>
Outputs:
n Bri
How can I remove three characters at the end of a string in PHP?
Just do:
echo substr($string, 0, -3);
You don't need to use a strlen
call, since, as noted in the substr documentation:
If length is given and is negative, then that many characters will be omitted from the end of string
Remove first 4 characters of a string with PHP
You could use the substr
function to return a substring starting from the 5th character:
$str = "The quick brown fox jumps over the lazy dog."
$str2 = substr($str, 4); // "quick brown fox jumps over the lazy dog."
Remove trailing delimiting character from a delimited string
Contrary to the question asked, rtrim()
will remove any number of characters, listed in the second argument, from the end of the string. In case you expect just a single comma, the following code would do:
$newarraynama = rtrim($arraynama, ",");
But in my case I had 2 characters, a comma and a space, so I had to change to
$newarraynama = rtrim($arraynama, " ,");
and now it would remove all commas and spaces from the end of the string, returning a, b, c, d, e
either from a, b, c, d, e,
, a, b, c, d, e,,,
, a, b, c, d, e,
or a, b, c, d, e , ,, , ,
But in case there could be multiple commas but you need to remove only the last one, then rtrim()
shouldn't be used at all - see other answers for the solution that directly answers the question.
However, rtrim()
could be a good choice if you don't know whether the extra character could be present or not. Unlike substr
-based solutions it will return a, b, c, d, e
from a, b, c, d, e
Remove first and last char from string
Using trim
:
trim($dataList, '*');
This will remove all *
characters (even if there are more than one!) from the end and the beginning of the string.
Remove a string from the beginning of a string
Plain form, without regex:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
if (substr($str, 0, strlen($prefix)) == $prefix) {
$str = substr($str, strlen($prefix));
}
Takes: 0.0369 ms (0.000,036,954 seconds)
And with:
$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
$str = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $str);
Takes: 0.1749 ms (0.000,174,999 seconds) the 1st run (compiling), and 0.0510 ms (0.000,051,021 seconds) after.
Profiled on my server, obviously.
Regex remove first character of three or lesser character words in a string
Without capturing groups,
(?<= |^)\+(?=\w{1,3}\b)
DEMO
Your final PHP code would be,
<?php
$string = '+a +string +of +words +with +different +lengths';
$pattern = "~(?<= |^)\+(?=\w{1,3}\b)~";
$replacement = "";
echo preg_replace($pattern, $replacement, $string);
?>
Output:
a +string of +words +with +different +lengths
Explanation:
(?<= |^)\+(?=\w{1,3}\b)
(?<= |^)
Positive lookbehind to look only after the starting point or space.\+
A literal + sign(?=\w{1,3}\b)
The characters following the+
sign must be (1 to 3) word characters which is again followed by a boundary charaqcter.
How to remove the leading character from a string?
To remove every :
from the beginning of a string, you can use ltrim:
$str = '::f:o:';
$str = ltrim($str, ':');
var_dump($str); //=> 'f:o:'
How can i cut the first 11 characters from a string?
Read the manual.
substr($foto_filename, 11);
Related Topics
How to Define an Empty Object in PHP
PHP Prepend Leading Zero Before Single Digit Number, On-The-Fly
Prevent Nginx 504 Gateway Timeout Using PHP Set_Time_Limit()
How to Explode and Trim Whitespace
Get Woocommerce Product Categories from Wordpress
How to Determine the Memory Footprint (Size) of a Variable
Laravel 4: How to "Order By" Using Eloquent Orm
How to Remove Part of a String in PHP
Ip Address Storing in MySQL Database Using PHP
Insert Current Date in Datetime Format MySQL
Enabling Postgresql Support in PHP on MAC Os X
Why Do I Have to Run "Composer Dump-Autoload" Command to Make Migrations Work in Laravel
Like Query Using Multiple Keywords from Search Field Using Pdo Prepared Statement
Mysqli_Query() Expects at Least 2 Parameters, 1 Given In
Calling Function Inside Preg_Replace Thats Inside a Function