php-general Digest 25 Apr 2011 19:45:32 -0000 Issue 7283

Topics (messages 312468 through 312483):

Re: str_replace
        312468 by: Daniel Brown
        312469 by: Daniel Brown
        312470 by: Adam Richardson
        312471 by: Stuart Dallas
        312474 by: Richard Quadling
        312483 by: Jim Lucas

htaccess question
        312472 by: Al
        312473 by: Al

trouble using "is_file()"
        312475 by: Jim Giner
        312476 by: Stuart Dallas
        312477 by: Jim Giner
        312478 by: Stuart Dallas
        312479 by: Jim Giner
        312480 by: Stuart Dallas
        312481 by: Jim Giner
        312482 by: Stuart Dallas

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Sun, Apr 24, 2011 at 11:44, Ron Piggott
<ron.pigg...@actsministries.org> wrote:
>
> I am trying to figure out a syntax that will replace each instance of % with
> a different letter chosen randomly from the string $puzzle_filler.
> $puzzle_filler is populated with the letters of the alphabet, roughly in the
> same ratio as they are used.
>
> This syntax replaces each instance of % with the same letter:
>
> $puzzle[$i] = str_replace ( "%" , ( substr ( $puzzle_filler , rand(1,98) , 1
> ) ) , $puzzle[$i] );
>
> Turning this:
>
> %ECARBME%TIPLUP%%%%%%%E%%
>
> Into:
>
> uECARBMEuTIPLUPuuuuuuuEuu
>
> Is there a way to tweak my str_replace so it will only do 1 % at a time, so
> a different replacement letter is selected?
>
> This is the syntax specific to choosing a replacement letter at random:
>
> substr ( $puzzle_filler , rand(1,98) , 1 );

    I just mocked this up now, and only tested it twice.  It's not the
most elegant solution, and probably shouldn't be used in high-demand
situations, but it should at least serve to get you started.

<?php

$str = '%ECARBME%TIPLUP%%%%%%%E%%';
$chars = 'abcdefghijklmnopqrstuvwxyz';

echo multi_replace($str,'%',$chars).PHP_EOL; // Case-sensitive,
random, straight replace
echo multi_replace($str,'%',$chars,0,1,1).PHP_EOL; //
Case-insensitive, random, randomg casing

function 
multi_replace($str,$targ,$chars,$sens=true,$random=true,$caserand=false)
{

        // Loop through while $targ is still found in $str
        while (strstr($str,$targ) !== false) {

                // If we're randomizing, pick the character; else the
first (or only)
                if ($random == true && !is_null($random)) {
                        $replace = $chars[mt_rand(0,(strlen($chars) - 1))];
                } else {
                        $replace = substr($chars,0,1);
                }

                // If we want random casing, do that; else make no change
                if ($caserand == true && !is_null($caserand)) {
                        if (mt_rand(0,1) === 0) {
                                $replace = strtolower($replace);
                        } else {
                                $replace = strtoupper($replace);
                        }
                }

                // If we don't want case sensitivity, set the regexp modifier
                $mod = $sens == false || is_null($sens) ? 'i' : '';

                // Perform the match and replace only one character now
                $str = preg_replace('/'.$targ.'/U'.$mod,$replace,$str,1);

        } // End of loop

        // Return the modified string
        return $str;
}
?>

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--- End Message ---
--- Begin Message ---
On Sun, Apr 24, 2011 at 12:33, Daniel Brown <danbr...@php.net> wrote:
>
>    I just mocked this up now, and only tested it twice.  It's not the
> most elegant solution, and probably shouldn't be used in high-demand
> situations, but it should at least serve to get you started.

    And since email wrecks code formatting, it's also up here:

        http://links.parasane.net/nth2

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--- End Message ---
--- Begin Message ---
On Sun, Apr 24, 2011 at 11:44 AM, Ron Piggott <
ron.pigg...@actsministries.org> wrote:

>
> I am trying to figure out a syntax that will replace each instance of %
> with a different letter chosen randomly from the string $puzzle_filler.
> $puzzle_filler is populated with the letters of the alphabet, roughly in the
> same ratio as they are used.
>
> This syntax replaces each instance of % with the same letter:
>
> $puzzle[$i] = str_replace ( "%" , ( substr ( $puzzle_filler , rand(1,98) ,
> 1 ) ) , $puzzle[$i] );
>
> Turning this:
>
> %ECARBME%TIPLUP%%%%%%%E%%
>
> Into:
>
> uECARBMEuTIPLUPuuuuuuuEuu
>
> Is there a way to tweak my str_replace so it will only do 1 % at a time, so
> a different replacement letter is selected?
>
> This is the syntax specific to choosing a replacement letter at random:
>
> substr ( $puzzle_filler , rand(1,98) , 1 );
>
> Thanks for your help.
>
> Ron
>
> The Verse of the Day
> “Encouragement from God’s Word”
> http://www.TheVerseOfTheDay.info
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Quick guess trying to be fairly performant:

function fill_puzzle($puzzle, $puzzle_filler) {
$puzzle_length = strlen($puzzle);
$puzzle_filler_length = strlen($puzzle_filler);
 for ($i = 0; $i < $puzzle_length; $i++) {
if ($puzzle[$i] == '%') {
$puzzle[$i] = $puzzle_filler[mt_rand(0, ($puzzle_filler_length - 1))];
}
}
 return $puzzle;
}

echo fill_puzzle($puzzle = "%ECARBME%TIPLUP%%%%%%%E%%", $puzzle_filler =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

Happy Easter :)

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com

--- End Message ---
--- Begin Message ---
On Sunday, 24 April 2011 at 16:44, Ron Piggott wrote:

> I am trying to figure out a syntax that will replace each instance of % with 
> a different letter chosen randomly from the string $puzzle_filler. 
> $puzzle_filler is populated with the letters of the alphabet, roughly in the 
> same ratio as they are used.
> 
> This syntax replaces each instance of % with the same letter:
> 
> $puzzle[$i] = str_replace ( "%" , ( substr ( $puzzle_filler , rand(1,98) , 
> 1 ) ) , $puzzle[$i] );
> 
> Turning this:
> 
> %ECARBME%TIPLUP%%%%%%%E%%
> 
> Into:
> 
> uECARBMEuTIPLUPuuuuuuuEuu
> 
> Is there a way to tweak my str_replace so it will only do 1 % at a time, so 
> a different replacement letter is selected?
> 
> This is the syntax specific to choosing a replacement letter at random:
> 
> substr ( $puzzle_filler , rand(1,98) , 1 );
> 
> Thanks for your help.

I would probably go with http://php.net/preg_replace_callback

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/





--- End Message ---
--- Begin Message ---
On 24 April 2011 16:44, Ron Piggott <ron.pigg...@actsministries.org> wrote:
>
> I am trying to figure out a syntax that will replace each instance of % with
> a different letter chosen randomly from the string $puzzle_filler.
> $puzzle_filler is populated with the letters of the alphabet, roughly in the
> same ratio as they are used.
>
> This syntax replaces each instance of % with the same letter:
>
> $puzzle[$i] = str_replace ( "%" , ( substr ( $puzzle_filler , rand(1,98) , 1
> ) ) , $puzzle[$i] );
>
> Turning this:
>
> %ECARBME%TIPLUP%%%%%%%E%%
>
> Into:
>
> uECARBMEuTIPLUPuuuuuuuEuu
>
> Is there a way to tweak my str_replace so it will only do 1 % at a time, so
> a different replacement letter is selected?
>
> This is the syntax specific to choosing a replacement letter at random:
>
> substr ( $puzzle_filler , rand(1,98) , 1 );
>
> Thanks for your help.
>
> Ron
>
> The Verse of the Day
> “Encouragement from God’s Word”
> http://www.TheVerseOfTheDay.info
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Something like this ...

<?php
$s_Text = '%ECARBME%TIPLUP%%%%%%%E%%';
$s_PuzzleFilter = '0123456789';
echo preg_replace_callback(
  '`%`',
  function() use($s_PuzzleFilter) {
    return substr($s_PuzzleFilter, mt_rand(0, strlen($s_PuzzleFilter)
- 1), 1);},
  $s_Text
);


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
On 4/24/2011 8:44 AM, Ron Piggott wrote:
> 
> I am trying to figure out a syntax that will replace each instance of % with a
> different letter chosen randomly from the string $puzzle_filler. 
> $puzzle_filler
> is populated with the letters of the alphabet, roughly in the same ratio as 
> they
> are used.
> 
> This syntax replaces each instance of % with the same letter:
> 
> $puzzle[$i] = str_replace ( "%" , ( substr ( $puzzle_filler , rand(1,98) , 1 
> ) )
> , $puzzle[$i] );
> 
> Turning this:
> 
> %ECARBME%TIPLUP%%%%%%%E%%
> 
> Into:
> 
> uECARBMEuTIPLUPuuuuuuuEuu
> 
> Is there a way to tweak my str_replace so it will only do 1 % at a time, so a
> different replacement letter is selected?
> 
> This is the syntax specific to choosing a replacement letter at random:
> 
> substr ( $puzzle_filler , rand(1,98) , 1 );
> 
> Thanks for your help.
> 
> Ron
> 
> The Verse of the Day
> “Encouragement from God’s Word”
> http://www.TheVerseOfTheDay.info
> 

How about something simple like this?

<?php

$input = '%ECARBME%TIPLUP%%%%%%%E%%';

$random_chars = range('a', 'z');

echo 'Before: '.$input.PHP_EOL;

while ( ($pos = strpos($input, '%') ) !== false )
    $input[$pos] = $random_chars[array_rand($random_chars)];

echo 'After: '.$input.PHP_EOL;

?>


--- End Message ---
--- Begin Message --- I want to restrict access to all files except one on my site and in parent dir. Thought this should work; but it doesn't.

<Files *>
Order Deny,Allow
Deny from all
Allow from xx.36.2.215
</Files>

xx.36.2.215 is actual value IP

This file makes a captcha image and is called with
<img src="makeScodeImg.php" alt="missing img file"  /> in file /dir/control.php

makeScodeImg.php is=> /dir/includes/makeScodeImg.php

Works fine if "allow all" just for testing

Thanks

--- End Message ---
--- Begin Message ---


On 4/24/2011 5:48 PM, Al wrote:
I want to restrict access to all files except one on my site and in parent dir.
Thought this should work; but it doesn't.

<Files *>
Order Deny,Allow
Deny from all
Allow from xx.36.2.215
</Files>

xx.36.2.215 is actual value IP

This file makes a captcha image and is called with
<img src="makeScodeImg.php" alt="missing img file" /> in file /dir/control.php

makeScodeImg.php is=> /dir/includes/makeScodeImg.php

Works fine if "allow all" just for testing

Thanks

Whoops. I meant to say I want to restrict access to all files in a directory [/includes] except allow access to makeScodeImg.php from one in the parent dir/control.php
--- End Message ---
--- Begin Message ---
I've got a d/e screen that collects a path and a file name. My script then 
attempts to verify that the file exists before moving on to do things with 
it.
I've displayed the combined fields and they are showing correctly - ie, 
"c:\temp\emax.pdf"  as well as "c:/temp/emax.pdf" .  But when I use that var 
in "is_file($fname)" I get a false returned.  But I know it exists there.

Never used this function before, so I'm probably doing something stupid. 
btw - running on an XP machine with php 5.2 



--- End Message ---
--- Begin Message ---
On Monday, 25 April 2011 at 00:26, Jim Giner wrote:
I've got a d/e screen that collects a path and a file name. My script then 
> attempts to verify that the file exists before moving on to do things with 
> it.
> I've displayed the combined fields and they are showing correctly - ie, 
> "c:\temp\emax.pdf" as well as "c:/temp/emax.pdf" . But when I use that var 
> in "is_file($fname)" I get a false returned. But I know it exists there.
> 
> Never used this function before, so I'm probably doing something stupid. 
> btw - running on an XP machine with php 5.2 

Are you expecting it to check that the file exists on the client or on the 
server?

Remember that PHP runs on the server and cannot access the client's file system.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/






--- End Message ---
--- Begin Message ---
Hmmm...  did not realize that.  From all the code I examined I thought it 
was looking at the client.

So how does one do a file upload from the client to the host?  Preferably 
with ftp rather than http. 



--- End Message ---
--- Begin Message ---
On Monday, 25 April 2011 at 01:07, Jim Giner wrote:
Hmmm... did not realize that. From all the code I examined I thought it 
> was looking at the client.
> 
> So how does one do a file upload from the client to the host? Preferably 
> with ftp rather than http.

FTP is a completely separate system from PHP, so it's a bit more involved to 
set up but it can be done. I know there are a few flash components that work 
this way - Google can help you find them. Why would you prefer to use FTP over 
HTTP?

As for the HTTP POST method... 
http://www.php.net/manual/en/features.file-upload.post-method.php

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/



--- End Message ---
--- Begin Message ---
because I've seen many comments in my research that ftp is better for larger 
files. I'm trying to upload photos so I know that they can be big. 



--- End Message ---
--- Begin Message ---
On Monday, 25 April 2011 at 01:24, Jim Giner wrote:
because I've seen many comments in my research that ftp is better for larger 
> files. I'm trying to upload photos so I know that they can be big. 

Considering the number of websites out there that use HTTP POST for uploading 
photos I really don't think you need to worry about it. Code it defensively, 
assume things will go wrong, and it will work as well for you as it does for 
them. In the grand scheme of things photo files are relatively small.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/





--- End Message ---
--- Begin Message ---
Well, I'll take your word for it.  As well as that of all the examples on 
Google that come up when looking for "php ftp uploads" which are NOT doing 
ftp uploads at all.  Seems that many people are calling the 'move' of their 
file from one host folder to another an 'upload'.  Thanks! 



--- End Message ---
--- Begin Message ---
You're probably seeing references to the move_uploaded_file PHP function which 
is used when processing an HTTP POST file upload. However, the fact that a 
simple query like that didn't give you anything related should tell you how 
often FTP upload is integrated into a website. Modify your search terms a bit 
and you'll get a whole bunch of java applets and flash components that can do 
FTP upload, but getting them cleanly integrated into a web interface is usually 
more effort than it's worth.

Don't take my word for it... Flickr uses HTTP. Facebook uses HTTP. They both 
use it for video too! Hell, even YouTube uses HTTP! Every other site I've ever 
come across to which you can upload photos and videos used HTTP. They usually 
do it via flash or java with nice looking progress indicators, but it's still 
HTTP. The only time I've seen FTP as an option was on a photo archiving site 
which was expecting you to upload a very large number of RAW files at a time - 
we're talking hundreds of megs.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

On Monday, 25 April 2011 at 01:32, Jim Giner wrote: 
> Well, I'll take your word for it. As well as that of all the examples on 
> Google that come up when looking for "php ftp uploads" which are NOT doing 
> ftp uploads at all. Seems that many people are calling the 'move' of their 
> file from one host folder to another an 'upload'. Thanks! 
> 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


--- End Message ---

Reply via email to