Separate string from end to start with substr php

Asked

Viewed 378 times

1

Hello, I come across strings like that:

Emirates EK 262

Air Canada AC 091

Lan Airlines LA 761

I know that from the end to the beginning the amount does not change, IE, if I want to get the name first name, regardless of whether it is a compound name or not, I start counting from the end part forward, I will not have mistakes.

I need to get the name without the 2 digit and 3 digit number. I mean, in my place, I know the position I get is -7. Now the problem is how do I get what’s ahead of this, from the end to the beginning?

$mais_detalhes['operado_por'] = substr($trecho[4],-7,*O que coloco aqui?);
  • What should the output string be? maybe you don’t even need the substr()

  • 1

    Give an example of what you need. Before and After. Reading what you wrote I couldn’t understand what you want.

2 answers

3


If I understand correctly you want "the whole string minus the last 7 characters".

According to the documentation:

If length [the third parameter] is given and is negative, then this amount of characters will be omitted from the end of the string.

Thus use:

substr("Lan Airlines LA 761", 0, -7);
  • 1

    kk, I managed to resolve and it was that way anyway, but still, thank you.

1

You can use the str_replace after picking up the string from the end with the substr

trim(str_replace(substr($value, -7), '', $value))

Results:

Emirates

Air Canada

Lan Airlines

Obs: change the variable $value by its string variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.