Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How would I parse this string in PHP

Here's my string: id:twitter.com:538757246

I've got to parse it so I can grab just the number.

How?
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This does work:

$mystring = "id:twitter.com:538757246";

$myarray=explode(":",$mystring);

$thenumber = $myarray[2];
echo $thenumber;
Lukasz, please do not just copy a previous person's answer and present it as your own.
Hi, I know you have an accepted solution already, but just a reminder:
For performance and resource effectiveness (although it makes not much difference in your case), the following would be a better solution:

$thenumber = substr(strrchr($mystring, ":"), 1);
echo $thenumber;

Open in new window

@gr8gonzo
I'm sorry, it was not there when I started writing an answer :)
Therefore it is not copied - there are not many possible simple answers to that question...
@Lukasz -

Even if you did the explode() and index method, the chances of using the EXACT same variable names, EXACT same naming prefix changes ($my changing to $the), the EXACT same line spacing, the EXACT same -different- spacing around = signs ($myarray=explode vs. $mystring = "id...) and commas, AND posting a full 7 minutes after Gary's post and claiming his wasn't there when you started writing a 5-line answer...

Put two developers in a room and tell them to write 2 lines of code to solve a simple problem and they'll almost always have different variable names at the very least.

So come on now...
OK, please do not get me wrong ... let me explain. I think I know why that happened.
I thought that the code was linked to the question - that's why I probably copied that and pasted into my editor.
That is why I wrote "This does work" meaning "This DOES work".
I am not new here - please take a look at my profile. That is not from copying others code snippets.
If that does not convince you, please forgive me for just copying YOUR work, it was not done deliberately. My head was somewhere else.