finding first instance

Busy's picture

He has: 6,151 posts

Joined: May 2001

Brain gone dead

$str = "re: this is a reply";
$pieces = explode("re:", $str);
echo $pieces[0]; // re:

problem:
explode can be anywhere in the string - need it to be first instance only, not pick up "this is not a re:"
Also I don't think explode picks up the ':'

Like I said, brain gone dead so any advise or helpful suggestions welcomed

Thanks

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Sounds like Perl would be perfect for this...

I'm not nearly as familiar with PHP, but I know in Perl you might have to put a backslash before the ":" to get Perl to recognize it.

dk01's picture

He has: 516 posts

Joined: Mar 2002

Check out the strpos() function and the substr() function.

Together I am sure they can do what you are looking for.

-dk

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I'm missing what you are actually trying to get from this example. (hey I just woke up, so I might be the brain dead one right now Wink

From your example you are just wanting to see if it starts with RE: or RE: is one of the things you are looking for? Are you just needing to get up to the first colon if it exists (so you can get RE: and FWD: ?)

-Greg

Busy's picture

He has: 6,151 posts

Joined: May 2001

Thanks dk01, could be what I was looking for.

Greg K, am trying to find if the strings starts with 're:' (as in reply), but not return a result if included elsewhere in the string.

Busy's picture

He has: 6,151 posts

Joined: May 2001

Solution:

$str = "re: this is the topic";
$re = substr("$str", 0, 3);
if($re == 're:')
{
echo "this string starts with re:";
}

dk01's picture

He has: 516 posts

Joined: Mar 2002

Remember to use the trim() function too just incase there is a leading space Wink

Glad to see you got what you were looking for!

-dk

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.