Solved

php find value in a string

Posted on 2014-04-14
5
486 Views
Last Modified: 2014-04-14
I have the following line of string:

** This. ion:1908er --- ion3, 237390, --- ion3, 2373104543, ---.......


In this line there are many "ion3" i need to find the value after the first ion3 that in this case is 237390. This number can have any number of digits, like the second ion3, but i only need the number of the first one that appears in the string. Always are a "," after ion3 then a space and after the number another ","
0
Comment
Question by:joyacv2
  • 2
  • 2
5 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 39999631
strpos and substr are your friends:

e.g.

$pos1 = strpos($the_string, "ion3, ") + 6;  // Add 6 for the length of the "ion3, "
$pos2 = strpos($the_string, ",", $pos1);
$result = substr($the_string, $pos1, $pos2 - $pos1);

Open in new window

0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 39999633
You can use a regular expression:
preg_match ( '/ion3,\s*(\d+)/m' , '** This. ion:1908er --- ion3, 237390, --- ion3, 2373104543, ---.......', $matches)

Open in new window

Your number will be in $matches[1].

HTH,
Dan
0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 39999916
both solutions work perfect!!!!!!
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39999921
Yup. And mine does not care how many spaces you have after the "," :)

Glad I could help!
0
 
LVL 1

Author Comment

by:joyacv2
ID: 39999924
perfect!!! this is perfect for other of my codes!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 dynamically set the form action using jQuery.

777 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