Exracting characters from a string & Detecting blank fie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
clang
Forum Newbie
Posts: 11
Joined: Tue Jun 12, 2007 2:28 pm

Exracting characters from a string & Detecting blank fie

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

<?php
if (preg_match('#^(/data/[a-z\d]{1,5}/).*$#i', "/data/fs1/HFtest20070724205643.pdf", $match))
{
    echo $match[1];
}
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Code: Select all

echo dirname('/data/fs1/HFtest20070724205643.pdf');
clang
Forum Newbie
Posts: 11
Joined: Tue Jun 12, 2007 2:28 pm

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Exracting characters from a string & Detecting blank

Post 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']))
{
    ....
}
clang
Forum Newbie
Posts: 11
Joined: Tue Jun 12, 2007 2:28 pm

Re: Exracting characters from a string & Detecting blank

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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().
clang
Forum Newbie
Posts: 11
Joined: Tue Jun 12, 2007 2:28 pm

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: Exracting characters from a string & Detecting blank

Post 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

Code: Select all

isset($_POST['foo'])
then remove leading and trailing spaces

Code: Select all

trim($_POST['foo'])
and then check if there's anything left (count of chars is more than zero)

Code: Select all

strlen() > 0
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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Exracting characters from a string & Detecting blank

Post by superdezign »

And FYI clang, the "> 0" is unnecessary.
clang
Forum Newbie
Posts: 11
Joined: Tue Jun 12, 2007 2:28 pm

Re: Exracting characters from a string & Detecting blank

Post 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?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
clang
Forum Newbie
Posts: 11
Joined: Tue Jun 12, 2007 2:28 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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).
Post Reply