Page 1 of 1
Exracting characters from a string & Detecting blank fie
Posted: Tue Jul 24, 2007 9:29 pm
by clang
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.
Posted: Tue Jul 24, 2007 9:45 pm
by Benjamin
Code: Select all
<?php
if (preg_match('#^(/data/[a-z\d]{1,5}/).*$#i', "/data/fs1/HFtest20070724205643.pdf", $match))
{
echo $match[1];
}
Posted: Wed Jul 25, 2007 3:29 am
by miro_igov
Code: Select all
echo dirname('/data/fs1/HFtest20070724205643.pdf');
Posted: Wed Jul 25, 2007 1:57 pm
by clang
miro_igov wrote:Code: Select all
echo dirname('/data/fs1/HFtest20070724205643.pdf');
That's perfect. Thank you. And thanks also astions.
So that solves problem 1. Any suggestions about problem 2?
Re: Exracting characters from a string & Detecting blank
Posted: Wed Jul 25, 2007 2:10 pm
by superdezign
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
Posted: Wed Jul 25, 2007 2:27 pm
by clang
superdezign wrote:
Code: Select all
if(!empty(trim($_POST['foo']))
{
....
}
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.
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.
}
Thanks every one
Posted: Wed Jul 25, 2007 2:35 pm
by superdezign
Oh yeah, empty is weird like that.
Code: Select all
$_POST['foo'] = trim($_POST['foo']);
if(!empty($_POST['foo']))
{
....
}
Don't use ereg_replace. This isn't a time for regular expressions. You'd be better of with str_replace().
Posted: Wed Jul 25, 2007 2:38 pm
by clang
superdezign wrote:
Don't use ereg_replace. This isn't a time for regular expressions. You'd be better of with str_replace().
Why so?
Posted: Wed Jul 25, 2007 2:42 pm
by superdezign
clang wrote:superdezign wrote:
Don't use ereg_replace. This isn't a time for regular expressions. You'd be better of with str_replace().
Why so?
It's a waste of memory when you are performing simple operations.
Re: Exracting characters from a string & Detecting blank
Posted: Wed Jul 25, 2007 5:17 pm
by stereofrog
clang wrote:Is there a way to see if the field contains no characters other than blank spaces?
First, you need to check if the field does exist at all
then remove leading and trailing spaces
and then check if there's anything left (count of chars is more than zero)
putting it all together
Code: Select all
if(isset($_POST['foo']) && strlen(trim($_POST['foo'])) > 0)
/// $_POST['foo'] is given and contains at least one non-space character
Re: Exracting characters from a string & Detecting blank
Posted: Wed Jul 25, 2007 7:14 pm
by superdezign
And FYI clang, the "> 0" is unnecessary.
Re: Exracting characters from a string & Detecting blank
Posted: Thu Jul 26, 2007 12:52 pm
by clang
stereofrog wrote:
putting it all together
Code: Select all
if(isset($_POST['foo']) && strlen(trim($_POST['foo'])) > 0)
/// $_POST['foo'] is given and contains at least one non-space character
It's hard for me to believe that is more efficient than
Code: Select all
$foo=ereg_replace( ' +', '', $foo);
if(!empty($foo))
...
Even if using regular expressions take up more memory, we're talking the difference between one call to a function with one if statement to 3 function calls and 2 if statements.
Am I wrong?
Posted: Thu Jul 26, 2007 1:48 pm
by stereofrog
In general, readability in programming is much more important than efficiency. This is how one would read the first snippet:
Code: Select all
if foo is set and length of foo with whitespace stripped is more than zero, then ....
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.
Posted: Thu Jul 26, 2007 1:55 pm
by clang
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.
Posted: Thu Jul 26, 2007 5:46 pm
by superdezign
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.
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).