How to Get the Last 7 Characters of a PHP String

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 the last 4 characters in a string - Laravel

You can use:

substr ($getstring->string, -4)

Because of (-) it will start form the end of the string and it will take the next 4 characters.

Take care that first() it will return an object and not the string you want.

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 x characters of a string until I reach a specific character?

try this

<?php
$tmpArray = explode("-",$mystr);
echo $tmpArray[1];

?>

You might want to refer to explode function in php.

Get at most last n characters from string using PHP substr?

substr("abcd", -4) returns "abcd"
substr("bcd", -4) returns "bcd"

This is the correct behaviour of substr().

There was a bug in the substr() function in PHP versions 5.2.2-5.2.6 that made it return FALSE when its first argument (start) was negative and its absolute value was larger than the length of the string.

The behaviour is documented.

You should upgrade your PHP to a newer version (5.6 or 7.0). PHP 5.2 is dead and buried more than 5 years ago.

Or, at least, upgrade PHP 5.2 to its latest release (5.2.17)


An elegant solution to your request (assuming you are locked with a faulty PHP version):

function substr52($string, $start, $length)
{
$l = strlen($string);
// Clamp $start and $length to the range [-$l, $l]
// to circumvent the faulty behaviour in PHP 5.2.2-5.2.6
$start = min(max($start, -$l), $l);
$length = min(max($start, -$l), $l);

return substr($string, $start, $length);
}

However, it doesn't handle the cases when $length is 0, FALSE, NULL or when it is omitted.

replace last seven characters of string

Should be...

$match = substr($string, -7);

... without the final -1. But in fact, it's far better done with...

$result = substr($string, 0, -7) . str_repeat('#', 7);

... or, more generic:

$coverWith = function($string, $char, $number) {
return substr($string, 0, -$number) . str_repeat($char, $number);
};

PHP: Get nth character from end of string

Just do this

  $rest = substr("abcdef", -3, 1); // returns 'd'

Get the first 5 and last 2 chars of a string

$str="moolti1";

$parts = str_split($str, 6);

Output the last few characters of a string - PHP / Yii

Use substr()

As documentation said:

If start is non-negative, the returned string will start at the
start'th position in string, counting from zero. For instance, in the
string 'abcdef', the character at position 0 is 'a', the character at
position 2 is 'c', and so forth.

If start is negative, the returned string will start at the start'th
character from the end of string.

So use negative value in second parameter

$data[0]['docno'] = substr($data[0]['docno'], -3);

Now use this in your Yii::$app->..... line

How to get first 5 characters from string

For single-byte strings (e.g. US-ASCII, ISO 8859 family, etc.) use substr and for multi-byte strings (e.g. UTF-8, UTF-16, etc.) use mb_substr:

// singlebyte strings
$result = substr($myStr, 0, 5);
// multibyte strings
$result = mb_substr($myStr, 0, 5);


Related Topics



Leave a reply



Submit