Solved

php find value in a string

Posted on 2014-04-14
5
481 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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
both solutions work perfect!!!!!!
0
 
LVL 34

Expert Comment

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

Glad I could help!
0
 
LVL 1

Author Comment

by:joyacv2
Comment Utility
perfect!!! this is perfect for other of my codes!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
posting form data 3 24
Get Javascript URL parameters 7 27
Php Array Diff 3 26
delete database record with modal 21 27
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now