phpstringsubst

Cutting two string elements


Alright so I am basically trying to cut 2 parts of a string

$string = "505 Space X 24";

Now what I am trying to do is basically cut everything besides Space X

So the output would be

"Space X"

Now, I dont know what will be the first number or the last one (the entire thing is dynamic) but it will always be

INT STRING INT

I tried using substr but I am not sure how to cut 2 parts of it.

I want to basically show only the string

EDIT: More info, I have to cut first 3 characters and last 3 characters from the string.


Solution

  • You could try to transform the string to an array, and get everythign but the first and last elements as they are numbers:

    $string = "505 Space X 24";
    
    $exploded = explode(' ', $string); // ['505', 'Space', 'X', '24']
    $str_elements = array_slice($exploded, 1, -1); // ['Space', 'X']
    $str = implode($str_elements, ' '); // 'Space X'
    var_dump($str);