Page 1 of 1

Pulling a substring out of the middle of a string

Posted: Thu Feb 18, 2010 2:55 pm
by mrlab
hey,
I'm trying to pull a substring out of this string I have created based off of my query...
What I want is to pull the three different variables and set them each to strings.
here's a photo of said string
Image
I can get the first and the last portions by using the following:

$vn = substr($vehicle, 0, strpos($vehicle, '~'));
&
$vp = substr($vehicle, strpos($vehicle, '|'));

but not the middle variable...
any help would be most appreciated
Thanks,

Re: Pulling a substring out of the middle of a string

Posted: Thu Feb 18, 2010 4:15 pm
by manohoo
mrlab, please be more specific. For instance, I want string "vehicle_name" to become "name"... or I want to remove "text" from "this_text"...

Re: Pulling a substring out of the middle of a string

Posted: Thu Feb 18, 2010 4:35 pm
by pickle
So you've got a string like: "Camaro~Sports Car|19995"?

I can think of 2 ways:

1)Convert the ~ to a | & explode

Code: Select all

$string = str_replace('~','|',$string);
list($vn,$vd,$vp) = explode('|',$string);
2) Use regex: /(.*)~(.*)\|(.*)/