[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# php find value in a string

Posted on 2014-04-14
Medium Priority
506 Views
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
Question by:joyacv2
[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
• 2
• 2

LVL 75

Assisted Solution

käµfm³d   👽 earned 1000 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);
``````
0

LVL 35

Accepted Solution

Dan Craciun earned 1000 total points
ID: 39999633
You can use a regular expression:
``````preg_match ( '/ion3,\s*(\d+)/m' , '** This. ion:1908er --- ion3, 237390, --- ion3, 2373104543, ---.......', \$matches)
``````
Your number will be in \$matches[1].

HTH,
Dan
0

LVL 1

Author Closing Comment

ID: 39999916
both solutions work perfect!!!!!!
0

LVL 35

Expert Comment

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

Glad I could help!
0

LVL 1

Author Comment

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

## Featured Post

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
###### Suggested Courses
Course of the Month14 days, left to enroll

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

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