sub string related

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

sub string related

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: sub string related

Post 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";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

Re: sub string related

Post 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
Post Reply