Asked  7 Months ago    Answers:  5   Viewed   39 times

I need to get the last character of a string. Say I have "testers" as input string and I want the result to be "s". how can I do that in PHP?

 Answers

85
substr("testers", -1); // returns "s"

Or, for multibytes strings :

substr("multibyte string…", -1); // returns "…"
Wednesday, March 31, 2021
 
Nil
answered 7 Months ago
Nil
46

A regex would be simplest:

$input = 'foo_left.jpg';
if(!preg_match('/_(left|right|center)/', $input, $matches)) {
    // no match
}

$pos = $matches[0]; // "_left", "_right" or "_center"

See it in action.

Update:

For a more defensive-minded approach (if there might be multiple instances of "_left" and friends in the filename), you can consider adding to the regex.

This will match only if the l/r/c is followed by a dot:

preg_match('/(_(left|right|center))./', $input, $matches);

This will match only if the l/r/c is followed by the last dot in the filename (which practically means that the base name ends with the l/r/c specification):

preg_match('/(_(left|right|center))\.[^\.]*$/', $input, $matches);

And so on.

If using these regexes, you will find the result in $matches[1] instead of $matches[0].

Wednesday, March 31, 2021
 
braindamage
answered 7 Months ago
88

Lots of things you could do.

s.substring(s.lastIndexOf(':') + 1);

will get everything after the last colon.

s.substring(s.lastIndexOf(' ') + 1);

everything after the last space.

String numbers[] = s.split("[^0-9]+");

splits off all sequences of digits; the last element of the numbers array is probably what you want.

Monday, June 28, 2021
 
dirigibleplum
answered 4 Months ago
61

This can't work properly. Stored with Unicode there are many more Characters than with ANSI. So if you "convert" to ANSI, you will loose lots of charackters.

http://php.net/manual/en/function.htmlentities.php

You can use Unicode (UTF-8) charset with htmlentities:

string htmlentities ( string $string [, int $flags = ENT_COMPAT [, string $charset [, bool $double_encode = true ]]] )

htmlentities($myString, ENT_COMPAT, "UTF-8"); should work.

Thursday, August 5, 2021
 
CoderGuy123
answered 3 Months ago
84

Here are several ways to remove trailing plus sign(s).

package main

import (
    "fmt"
    "strings"
)

func TrimSuffix(s, suffix string) string {
    if strings.HasSuffix(s, suffix) {
        s = s[:len(s)-len(suffix)]
    }
    return s
}

func main() {
    s := "a string ++"
    fmt.Println("s: ", s)

    // Trim one trailing '+'.
    s1 := s
    if last := len(s1) - 1; last >= 0 && s1[last] == '+' {
        s1 = s1[:last]
    }
    fmt.Println("s1:", s1)

    // Trim all trailing '+'.
    s2 := s
    s2 = strings.TrimRight(s2, "+")
    fmt.Println("s2:", s2)

    // Trim suffix "+".
    s3 := s
    s3 = TrimSuffix(s3, "+")
    fmt.Println("s3:", s3)
}

Output:

s:  a string ++
s1: a string +
s2: a string 
s3: a string +
Friday, September 10, 2021
 
Juriy
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :