php-windows Digest 24 Dec 2003 07:10:46 -0000 Issue 2055
Topics (messages 22421 through 22424):
Regular Expressions
22421 by: Gerardo Rojas
22422 by: Frank M. Kromann
Re: open directory and read the file
22423 by: Sven Schnitzke
22424 by: Daniel Crespo
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[EMAIL PROTECTED]
----------------------------------------------------------------------
--- Begin Message ---
How do I use preg_replace to replace a space (" ") with a comma (",")?
i am using:
preg_replace('/ /', ',', $string).
this isn't working.
--
Gerardo S. Rojas
mailto: [EMAIL PROTECTED]
--- End Message ---
--- Begin Message ---
Hi,
The replaced string is returned by the function. This works.
$str = "test test2";
$str2 = preg_replace('/ /', ',', $str);
If your are doing simple replacements like this you should use
str_replace() it's much faster!
$str = "test test2";
$str2 = str_replace(' ', ',', $str);
- Frank
> How do I use preg_replace to replace a space (" ") with a comma (",")?
>
> i am using:
>
> preg_replace('/ /', ',', $string).
>
> this isn't working.
>
>
> --
> Gerardo S. Rojas
> mailto: [EMAIL PROTECTED]
>
>
--- End Message ---
--- Begin Message ---
Simply do a
> while ($file = readdir($handle)){
and make your directory current inserting
chdir('c:\apache\htdocs');
just before the line where you finally check success of the fopen using
> if ($handle_file = fopen ($find_file, "r")) {
> ...
> }
> else print 'Unable to open file '.$find_file;
because most likely c:\apache\htdocs isn't the current dir when
executing the fopen (opendir does not make a dir current to fopen).
Alternately you might want to feed the complete pathname
(including dir) to the fopen.
Besides: in a case where you know the filename in advance you might
leave out the directory search completely, thus saving a lot of execution
time.
HTH
--
Sven
> -----Ursprüngliche Nachricht-----
> Von: Idur [SMTP:[EMAIL PROTECTED]
> Gesendet am: Dienstag, 23. Dezember 2003 18:42
> An: [EMAIL PROTECTED]
> Betreff: [PHP-WIN] open directory and read the file
>
> Hi there,
>
> I want read a directory and open a file inside the directory, for example
directory data contains of file:
> Computername.txt
> IPaddress.txt
> status.txt
>
> and i want search a file on the directory and open it, for example i want
search file IPaddress.txt and when file IPaddress.txt found, i want open
it, read and display the contains of the file.
>
> the script that i have is like this :
> <?php
> $find_file="name.txt";
> if ($handle = opendir('C:\apache\htdocs')){
> while (false !== ($file = readdir($handle))){
> if ($file == $find_file){
> echo " file found $file<br>";
>
> // i want to open the file, but i am confuse with the file
handle
> $handle_file = fopen ($find_file, "r");
> while (!feof ($handle_file)){
> $buffer = fgets($handle_file, 4096);
> echo "$buffer<br>";
> }
> }
> }
> closedir($handle);
> }
> ?>
> this script does'nt run correctly i have an error message, "Warning:
Supplied argument is not a valid File-Handle resource in
c:\apache\htdocs\fseek\readdir.php on line 11"
>
> I confuse with the file handle, because is different with opening file
like usually, what should i do...??? does any one can help me...??
>
> Thanx
> idur
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Photos - Get your photo on the big screen in Times Square
--- End Message ---
--- Begin Message ---
I think you have the problem in:
$handle_file = fopen ($find_file, "r");
while (!feof ($handle_file)){
$buffer = fgets($handle_file, 4096);
echo "$buffer<br>";
}
Right? Well, I think the next code could help you:
<? $cats = file("test.txt");
for($i=0; $i<count($cats); $i++) {
echo $cats[$i]."<br>";
}
?>
This prints each line of the text file. I hope it helps you.
"Idur" <[EMAIL PROTECTED]> escribió en el mensaje
news:[EMAIL PROTECTED]
> Hi there,
>
> I want read a directory and open a file inside the directory, for example
directory data contains of file:
> Computername.txt
> IPaddress.txt
> status.txt
>
> and i want search a file on the directory and open it, for example i want
search file IPaddress.txt and when file IPaddress.txt found, i want open it,
read and display the contains of the file.
>
> the script that i have is like this :
> <?php
> $find_file="name.txt";
> if ($handle = opendir('C:\apache\htdocs')){
> while (false !== ($file = readdir($handle))){
> if ($file == $find_file){
> echo " file found $file<br>";
>
> // i want to open the file, but i am confuse with the file
handle
> $handle_file = fopen ($find_file, "r");
> while (!feof ($handle_file)){
> $buffer = fgets($handle_file, 4096);
> echo "$buffer<br>";
> }
> }
> }
> closedir($handle);
> }
> ?>
> this script does'nt run correctly i have an error message, "Warning:
Supplied argument is not a valid File-Handle resource in
c:\apache\htdocs\fseek\readdir.php on line 11"
>
> I confuse with the file handle, because is different with opening file
like usually, what should i do...??? does any one can help me...??
>
> Thanx
> idur
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Photos - Get your photo on the big screen in Times Square
--- End Message ---