Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

php find value in a string

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
joyacv2
Asked:
joyacv2
  • 2
  • 2
2 Solutions
 
käµfm³d 👽Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
joyacv2Author Commented:
both solutions work perfect!!!!!!
0
 
Dan CraciunIT ConsultantCommented:
Yup. And mine does not care how many spaces you have after the "," :)

Glad I could help!
0
 
joyacv2Author Commented:
perfect!!! this is perfect for other of my codes!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now