Exracting characters from a string & Detecting blank fie
Moderator: General Moderators
Exracting characters from a string & Detecting blank fie
I have two questions. I would appreciate any help I can get.
1) I need to be able to take the string "/data/fs1/HFtest20070724205643.pdf" and extract only "/data/fs1/". Then problem I'm having is the "../fs1/.." part isn't a constant. It needs to have the ability to be fs2, fs3, or whatever else.
2)I am taking data in from some html forms ( I know, bad, but I don't have time to learn javascript or anything else) and am letting php do the data validation. I've already got it checking to see if it blank (ie $foo='') and I'm also checking to make sure it contains only numbers with this regex string preg_match("/[^0-9\ ]+$/",$number) (trailing/leading blank spaces are ok, spaces in the middle of the numbers are not). Is there a way to see if the field contains no characters other than blank spaces?
Thank you for the help. I'm new to php so these simple questions prove to be very difficult for me. If I have omitted any information you need to help with my problems, please let me know and I will post them promptly.
1) I need to be able to take the string "/data/fs1/HFtest20070724205643.pdf" and extract only "/data/fs1/". Then problem I'm having is the "../fs1/.." part isn't a constant. It needs to have the ability to be fs2, fs3, or whatever else.
2)I am taking data in from some html forms ( I know, bad, but I don't have time to learn javascript or anything else) and am letting php do the data validation. I've already got it checking to see if it blank (ie $foo='') and I'm also checking to make sure it contains only numbers with this regex string preg_match("/[^0-9\ ]+$/",$number) (trailing/leading blank spaces are ok, spaces in the middle of the numbers are not). Is there a way to see if the field contains no characters other than blank spaces?
Thank you for the help. I'm new to php so these simple questions prove to be very difficult for me. If I have omitted any information you need to help with my problems, please let me know and I will post them promptly.
Code: Select all
<?php
if (preg_match('#^(/data/[a-z\d]{1,5}/).*$#i', "/data/fs1/HFtest20070724205643.pdf", $match))
{
echo $match[1];
}Code: Select all
echo dirname('/data/fs1/HFtest20070724205643.pdf');That's perfect. Thank you. And thanks also astions.miro_igov wrote:Code: Select all
echo dirname('/data/fs1/HFtest20070724205643.pdf');
So that solves problem 1. Any suggestions about problem 2?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Exracting characters from a string & Detecting blank
clang wrote:2)I am taking data in from some html forms ( I know, bad, but I don't have time to learn javascript or anything else) and am letting php do the data validation. I've already got it checking to see if it blank (ie $foo='') and I'm also checking to make sure it contains only numbers with this regex string preg_match("/[^0-9\ ]+$/",$number) (trailing/leading blank spaces are ok, spaces in the middle of the numbers are not). Is there a way to see if the field contains no characters other than blank spaces?
Code: Select all
if(!empty(trim($_POST['foo']))
{
....
}Re: Exracting characters from a string & Detecting blank
Thanks for the advice. Your solution actually gave me an error when I ran it. It didn't like trim being inside the empty() function.superdezign wrote:Code: Select all
if(!empty(trim($_POST['foo'])) { .... }
But no matter, as it pointed me in the right direction in the manual.
For any one else that should ever need it here is the solution I've found:
Code: Select all
$foo=" 111 22 333";
$foo=ereg_replace( ' +', '', $foo);
if(!empty($foo))
{
echo "pass";//$foo contains only characters, no blank spaces
}
else
{
echo "fail";//$foo contains no characters.
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Oh yeah, empty is weird like that.
Don't use ereg_replace. This isn't a time for regular expressions. You'd be better of with str_replace().
Code: Select all
$_POST['foo'] = trim($_POST['foo']);
if(!empty($_POST['foo']))
{
....
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Re: Exracting characters from a string & Detecting blank
First, you need to check if the field does exist at allclang wrote:Is there a way to see if the field contains no characters other than blank spaces?
Code: Select all
isset($_POST['foo'])Code: Select all
trim($_POST['foo'])Code: Select all
strlen() > 0Code: Select all
if(isset($_POST['foo']) && strlen(trim($_POST['foo'])) > 0)
/// $_POST['foo'] is given and contains at least one non-space character- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Exracting characters from a string & Detecting blank
And FYI clang, the "> 0" is unnecessary.
Re: Exracting characters from a string & Detecting blank
It's hard for me to believe that is more efficient thanstereofrog wrote: putting it all togetherCode: Select all
if(isset($_POST['foo']) && strlen(trim($_POST['foo'])) > 0) /// $_POST['foo'] is given and contains at least one non-space character
Code: Select all
$foo=ereg_replace( ' +', '', $foo);
if(!empty($foo))
...Am I wrong?
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
In general, readability in programming is much more important than efficiency. This is how one would read the first snippet:
As you can see, the natural language description and the code match 1:1. For the second snippet, it's not immediately obvious what is going on there. Someone else (or you a month later) will be quite confused reading this code.
Code: Select all
if foo is set and length of foo with whitespace stripped is more than zero, then ....- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
If you insist on using regular expressions, use preg_replace. I believe they're going to do away with ereg in PHP 6 (rumor, but it should be done anyway).clang wrote:Good point.
I document my code fairly well though, so I should be ok.
Thank you for the tip though. It's always interesting to me to find the different ways of solving the same problem, and comparing them with each other.