Solved

PHP MYSQL: How to select part of string?

Posted on 2008-06-10
8
1,696 Views
Last Modified: 2013-12-13
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
Comment
Question by:gingera
[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
  • 2
  • +2
8 Comments
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 200 total points
ID: 21754389
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
 
LVL 48

Accepted Solution

by:
hernst42 earned 300 total points
ID: 21754402
You can use a regular expression like

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

echo $value;
0
 
LVL 9

Expert Comment

by:michofreiha
ID: 21754435
strstr($xxxxx, '.0');
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 19

Expert Comment

by:LordOfPorts
ID: 21754440
Try:

$value = substr($url, strrpos($url, "."));
0
 
LVL 19

Expert Comment

by:LordOfPorts
ID: 21754485
Correction, ommitted 0 for start:

$value = substr($url, 0, strrpos($url, "."));
0
 

Author Comment

by:gingera
ID: 21755041
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
 
LVL 48

Expert Comment

by:hernst42
ID: 21757521
the solution of glummicus should also work, the others not
0
 

Author Closing Comment

by:gingera
ID: 31465899
THANKS!
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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