Separate string from end to end with substr php

1

Hello, I come across strings of this type:

  

Emirates EK 262

     

Air Canada AC 091

     

Lan Airlines LA 761

I know that from the end to the beginning the quantity does not change, ie if I want to get the name first name, regardless of whether or not it is a composite name, I start counting from the end part forward, which does not I will have errors.

I need to get the name without the prefix of 2 digits and the number of 3 digits. that is, in my substr, I know the position I get is -7. Now the problem is how do I get what's ahead of it, from the end to the beginning?

$mais_detalhes['operado_por'] = substr($trecho[4],-7,*O que coloco aqui?);
    
asked by anonymous 16.11.2015 / 14:28

2 answers

3

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

According to 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.

In this way use:

substr("Lan Airlines LA 761", 0, -7);
    
16.11.2015 / 14:38
1

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

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

Results:

  

Emirates

     

Air Canada

     

Lan Airlines

Obs: change the variable $value by its string variable

    
16.11.2015 / 14:37