Tomislav Nikolic
Tomislav Nikolic

Reputation: 193

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.

Upvotes: 1

Views: 91

Answers (1)

Jeremy D
Jeremy D

Reputation: 4855

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);

Upvotes: 2

Related Questions