Learn how to a build a cloud-first strategyRegister Now

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

PHP MYSQL: How to select part of string?

PHP MySQL


Hello,

How do I code in a PHP script to look for part of a string retrieved from MySQL database?


For example...

MySQL database field contents: (call this $url)
xxxxxxxxx=123.0
xxxxxxxxx=5161163.0
xxxxxxxxx=25523.0
xxxxxxxxx=11.0


I want to extract the value between = and .0
so I get
123
5161163
25523
11


So in PHP, we will have something like

$value = substring of $url

but I don't know how to limit the substring to be between = and .0


0
gingera
Asked:
gingera
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
glcumminsCommented:
Use the following two lines. At the end, your desired value will be stored in '$value':

list ($xxxxx, $floatNumber) = explode('=', $url);
list ($value, $decimalPortion) = explode('.', $floatNumber);
0
 
hernst42Commented:
You can use a regular expression like

preg_match('/=(.*)\.0/', $url, $m);
$value = $m[1];

echo $value;
0
 
michofreihaCommented:
strstr($xxxxx, '.0');
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LordOfPortsCommented:
Try:

$value = substr($url, strrpos($url, "."));
0
 
LordOfPortsCommented:
Correction, ommitted 0 for start:

$value = substr($url, 0, strrpos($url, "."));
0
 
gingeraAuthor Commented:
Thanks guys!

I have only tried hernst42's suggested solution, and it works for me.

I want to be fair in allocating points  to others who contributed correct solutions but I don't have the time to test out all the other suggestions at the moment.

Could hernst confirm if the other solutions will work?
0
 
hernst42Commented:
the solution of glummicus should also work, the others not
0
 
gingeraAuthor Commented:
THANKS!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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