p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: help me


Message #1 by "Wayne Frisch" <wfrisch@b...> on Tue, 19 Feb 2002 01:27:10
 I am new to PHP and have written some code to make life easier at work 

however I cannot get the following to work



I have a variable $userfile_name which contains the value 

c:\\temp\\scanx.jpg I want to create another variable that strips the path

down to scanx.jpg then renames it scanx.pdf any suggestions



thanks
Message #2 by "Hermawan Haryanto" <hermawan@h...> on Tue, 19 Feb 2002 08:37:14 +0700
Try this way :



<?php

$userfile_name="c:\\temp\\scanx.jpg";

$arr = explode("\\", $userfile_name);

$arr = explode(".", $arr[2]);

$userfile_name = $arr[0].".pdf";

print $userfile_name;

?>



Thanks



Hermawan Haryanto

hermawan@h...

----- Original Message -----

From: "Wayne Frisch" <wfrisch@b...>

To: "beginning php" <beginning_php@p...>

Sent: Tuesday, February 19, 2002 1:27 AM

Subject: [beginning_php] help me





> I am new to PHP and have written some code to make life easier at work

> however I cannot get the following to work

>

> I have a variable $userfile_name which contains the value

> c:\\temp\\scanx.jpg I want to create another variable that strips the path

> down to scanx.jpg then renames it scanx.pdf any suggestions

>

> thanks




$subst('Email.Unsub').

>



Message #3 by "Nikolai Devereaux" <yomama@u...> on Mon, 18 Feb 2002 17:53:41 -0800

The array way should work, but you could also (should?) use regular

expressions for string manipulation. =)



$filename    = "c:\\temp\\scanx.jpg"



$pattern     = "^.*\\\\([[:alnum:]]+)\\.jpg$";



$newfilename = ereg_replace($pattern, "\\1.pdf",  $filename);



// $newfilename is "scanx.pdf" now.



The regular expression will match a string with some alphanumeric sequence

of characters somewhere between a backslash and a .jpg at the end of a line.



This expression actually matches the entire line -- "^.*" matches all

non-newline chars at the beginning of the line.  All we're interested in

remembering is the filename just before the .jpg at the end of the line ($).



We match the entire line because we're replacing the matched text with

"\\1.pdf".  \\1 refers to the first parenthesized sequence... If our pattern

was "^(.*\\\\)([[:alnum:]])(\\.jpg)$", we'd be interested in \\2 instead...

get it?



Here is one instance where the php.net manual online is way inferior to most

good PHP books: http://www.php.net/regex





take care,



nik



> -----Original Message-----

> From: Wayne Frisch [mailto:wfrisch@b...]

> Sent: Tuesday, February 19, 2002 1:27 AM

> To: beginning php

> Subject: [beginning_php] help me

>

>

>  I am new to PHP and have written some code to make life easier at work

> however I cannot get the following to work

>

> I have a variable $userfile_name which contains the value

> c:\\temp\\scanx.jpg I want to create another variable that strips the path

> down to scanx.jpg then renames it scanx.pdf any suggestions

>

> thanks




> $subst('Email.Unsub').



Message #4 by "Lawrence" <lkrubner@g...> on Tue, 19 Feb 2002 17:07:23 -0500
$newVariable = basename("c:\\temp\\scanx.jpg ");

$endResult = str_replace("jpg", "pdf", "$newVariable");



Double check my syntax, but that would work. 



----- Original Message ----- 

From: "Wayne Frisch" <wfrisch@b...>

To: "beginning php" <beginning_php@p...>

Sent: Tuesday, February 19, 2002 1:27 AM

Subject: [beginning_php] help me





> I am new to PHP and have written some code to make life easier at work 

> however I cannot get the following to work

> 

> I have a variable $userfile_name which contains the value 

> c:\\temp\\scanx.jpg I want to create another variable that strips the path

> down to scanx.jpg then renames it scanx.pdf any suggestions

> 

> thanks








Message #5 by "Nikolai Devereaux" <yomama@u...> on Tue, 19 Feb 2002 13:59:55 -0800

> $newVariable = basename("c:\\temp\\scanx.jpg ");

> $endResult = str_replace("jpg", "pdf", "$newVariable");

> 

> Double check my syntax, but that would work. 



It would work, but that doesn't get rid of the 'c:\temp\' part.





Message #6 by "Lawrence" <lkrubner@g...> on Tue, 19 Feb 2002 21:23:00 -0500
From: "Nikolai Devereaux" <yomama@u...>

> > $newVariable = basename("c:\\temp\\scanx.jpg ");

> > $endResult = str_replace("jpg", "pdf", "$newVariable");

> >

> > Double check my syntax, but that would work.

>

> It would work, but that doesn't get rid of the 'c:\temp\' part



I'm not sure I understand you. The function basename() returns only the file name, not the path.

This works on Unix systems and it is what I often do. Does it not work on Windows?



This is what is says on www.php.net:





basename --  Returns filename component of path

Description

string basename ( string path [, string suffix])

Given a string containing a path to a file, this function will return the base name of the file. If

the filename ends in suffix this will also be cut off.



On Windows, both slash (/) and backslash (\) are used as path separator character. In other

environments, it is the forward slash (/).



$path = "/home/httpd/html/index.php";

$file = basename ($path);        // $file is set to "index.php"

$file = basename ($path,".php"); // $file is set to "index"







Message #7 by "Nikolai Devereaux" <yomama@u...> on Wed, 20 Feb 2002 08:49:07 -0800

> I'm not sure I understand you. The function basename() returns 

> only the file name, not the path.

> This works on Unix systems and it is what I often do. Does it not 

> work on Windows?



oops, my bad -- You're totally right.  Sorry about that.



nik



Message #8 by "Hermawan Haryanto" <hermawan@h...> on Thu, 21 Feb 2002 00:04:19 +0700
Want me to shoot you Nik ?... :))))



Thanks



Hermawan Haryanto

hermawan@h...

----- Original Message -----

From: "Nikolai Devereaux" <yomama@u...>

To: "beginning php" <beginning_php@p...>

Sent: Wednesday, February 20, 2002 11:49 PM

Subject: [beginning_php] Re: help me





>

> > I'm not sure I understand you. The function basename() returns

> > only the file name, not the path.

> > This works on Unix systems and it is what I often do. Does it not

> > work on Windows?

>

> oops, my bad -- You're totally right.  Sorry about that.

>

> nik

>

>




$subst('Email.Unsub').

>




  Return to Index