Page 1 of 1

sub string related

Posted: Thu Jan 14, 2010 10:00 am
by incubi
Hello,

I'm looking for the best way to extract a sub string from a string without knowing its location. For example,

$string = "Bob had a baby its a boy";

I want to get (a baby) out of it and discard the rest :)

All the options I found need to have its offset or needs a delimiter char.

Thanks,

incubi

Re: sub string related

Posted: Thu Jan 14, 2010 10:22 am
by AbraCadaver
This seems strange, as you know what you are looking for and are going to discard the rest, so why not just use what you're looking for? However, here's one way:

Code: Select all

$string = "Bob had a baby its a boy";
$find = "a baby";
 
$found = substr($string, strpos($string, $find), strlen($find));
Wouldn't it be easier just to say?

Code: Select all

$string = "a baby";

Re: sub string related

Posted: Thu Jan 14, 2010 10:53 am
by incubi
Actually you are right what I need is a section of text after that, and I don't know the value. That's easy enough to do. I think I'm losing it :crazy:
But thanks, its nice to know its easily done.

incubi