How to get the last char of a string in PHP?
substr("testers", -1); // returns "s"
Or, for multibyte strings :
mb_substr("multibyte string…", -1); // returns "…"
How can I get the last 7 characters of a PHP string?
Use substr()
with a negative number for the 2nd argument.
$newstring = substr($dynamicstring, -7);
From the php docs:
string substr ( string $string , int $start [, int $length ] )
If start is negative, the returned string will start at the start'th character from the end of string.
How to get position of last character in a string?
In PHP, as in most languages with zero-based indexing, the index of the last character in a string will be one less than the length of the string (strlen($string) - 1
).
Find last character of php string
You should look into regular expressions using something like preg_match()
An expression like this would match:
/([a-z])[^a-z]*$/i
It means:
([a-z])
Capture an a-z character (thei
at the end makes it case-insensitive)[^a-z]*$
followed by 0 or more non a-z characters until the end of the string
See an example.
PHP: Get nth character from end of string
Just do this
$rest = substr("abcdef", -3, 1); // returns 'd'
Last character of php string in php
for( $i = 0, $len = strlen( $string ); $i < $len; $i++ )
{
$char = $string[ $i ];
// do something with $char
}
... is one way to do it.
Another way, is simply using PHP function str_split()
(which creates an array of the individual characters of the string (unless you pass it the second argument $split_length
of a different length than the default)):
foreach( str_split( $string ) as $char )
{
// do something with $char
}
... etc.
Edit:
As an addendum to your comments to your question; use fgetcsv()
for parsing csv files.
What is the simplest of checking first and last character of a string in php
if($str[0] == 'a' && $str[strlen($str) - 1] == 'a') {
//do whatever you wanted
}
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
Related Topics
How to Post a Large Amount of Data Within PHP Curl Without Memory Overhead
Decoding Numeric HTML Entities via PHP
Wordwrap/Cut Text in HTML String
Transpose Multidimensional Array and Join Values with Commas
Zf2 + Doctrine 2 - Child-Level Discriminators with Class Table Inheritance
Best Way to Document Array Options in PHPdoc
How to Properly Display Chinese Characters in PHP
Importing Multiple CSV Files to MySQL Tables
Why Are $_Server["Php_Auth_User"] and $_Server["Php_Auth_Pw"] Not Set
How to Set an Absolute Include Path in PHP
How to Run Composer from Anywhere
PHP If Single or Double Equals
Check If a "Run-Time" Multidimensional Array Key Exists
How to Install All Required PHP Extensions for Laravel
PHP Object of Class Dateinterval Could Not Be Converted to String
Can't Find the File Created by Outfile in MySQL