Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How would I parse this string in PHP

Posted on 2014-09-25
8
Medium Priority
?
94 Views
Last Modified: 2014-09-28
Here's my string: id:twitter.com:538757246

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

How?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 40344438
$mystring = "id:twitter.com:538757246";

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

$thenumber = $myarray[2];

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 40344454
This does work:

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

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

$thenumber = $myarray[2];
echo $thenumber;
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40344609
Lukasz, please do not just copy a previous person's answer and present it as your own.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:Brian Tao
ID: 40345242
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

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 40348527
@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...
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40348665
@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...
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 40348732
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question