Solved

PHP MYSQL: How to select part of string?

Posted on 2008-06-10
8
1,690 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
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 24

Assisted Solution

by:glcummins
glcummins earned 200 total points
Comment Utility
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
Comment Utility
You can use a regular expression like

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

echo $value;
0
 
LVL 9

Expert Comment

by:michofreiha
Comment Utility
strstr($xxxxx, '.0');
0
 
LVL 19

Expert Comment

by:LordOfPorts
Comment Utility
Try:

$value = substr($url, strrpos($url, "."));
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Expert Comment

by:LordOfPorts
Comment Utility
Correction, ommitted 0 for start:

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

Author Comment

by:gingera
Comment Utility
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
Comment Utility
the solution of glummicus should also work, the others not
0
 

Author Closing Comment

by:gingera
Comment Utility
THANKS!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 count occurrences of each item in an array.
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 …

744 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