Jump to content

discomatt

Members
  • Posts

    1,943
  • Joined

  • Last visited

    Never

Everything posted by discomatt

  1. Jibberish has it. You must reset the internal pointer after $row = mysql_fetch_array($result); Remove it or try mysql_data_seek()
  2. Check the forums here http://www.geonames.org/ They know their stuff
  3. I've always liked this method: Generate random salt ( usually of the same length as the hash ) Chop the password and salt into 2 chunks. Mix chunks and hash. Split hash into 2 parts, mix with salt. You now have the salt stored in plain text, but it's theoretically impossible to extract it from the hash without knowing the chunk sizes and algorithm used. It's a little paranoid, but it's actually a very light script ( php is very quick and string manip ) requiring only 1 hash to check a password. Salting is VERY VERY important. I would argue a random salt is equally important. You're protecting your viewers passwords they might use elsewhere. Rainbow tables are becoming huge and easy to access.
  4. That's another neat concept, but that one is a hog, running about 10x slower than our previous functions using realistic depth, and nearly 15x slower than mine on unrealistic depths. I think I'm going to stick with either your or my original function, pending a little more speed testing. Thanks again for the time and thought, and helping me look outside the box
  5. I think the issue is the end user typing in double stars. Your best bet is to match \*++ in the regex that does the conversion.
  6. That works great! Neat way of doing it. Speed wise, the functions are near equal in real-life situations... I did trials using random data with an average depth ~5 and my function edged out yours by a negligible time. When doing unrealistic trials ( average depth ~100 ) your function ran about 3x faster than mine. Thanks for the help sasa!
  7. Here's a working example of what I'm doing on a broader scope <pre><?php /* 1 67 20 107 43 116 77 153 125 161 */ $data = <<<DATA {loop} Some data {loop} Nested data {loop} More nests! {/loop} {loop} Another deep nest {/loop} {/loop} {loop} Not quite so deep {/loop} {/loop} {loop} More data {loop} Foobar {/loop} {loop} Warmer {loop} Waaaarmer {loop} Too hot! {/loop} {/loop} {/loop} {loop} More depth {loop} this is getting stupid {/loop} {/loop} {/loop} DATA; $pairs = getPairs($data, '{loop}', '{/loop}'); foreach( $pairs as $pair ) echo substr( $data, $pair[0], $pair[1]-$pair[0] ) . "\n\n\n--------\n\n\n"; function getPairs( $data, $openTag, $closeTag ) { // Grab opening and closing offsets $opening = getOffsets( $data, $openTag ); $closing = getOffsets( $data, $closeTag, FALSE ); // Verify proper formatting if( ($size = count($opening)) !== count($closing) ) die( "Invalid syntax detected" ); // Placeholder $offsets = array(); // Separate nests for( $i = 1; $i < $size; $i++ ) { // Set initial nest key static $last = 0; // Check to see if a new nest has begun if( isset($opening[$i+1]) === FALSE || $opening[$i+1] > $closing[$i] ) { // Send nest to parser and gather results $offsets = array_merge( $offsets, parseNest(array_slice($opening,$last,$i-$last+1), array_slice($closing,$last,$i-$last+1)) ); // Update nest key to the start of the next nest $last = $i+1; } } return $offsets; } function getOffsets( $data, $tag, $open = TRUE ) { // Placeholder $return = array(); // Start at beginning of string $offset = 0; // Loop through string grabbing offsets while( ($c = strpos($data, $tag, $offset)) !== FALSE ) { // Update search offset $offset = strpos( $data, $tag, $c ) + 1; // Remove tag offset and add to placeholder $return[] = $offset - 1 + ( $open == TRUE ? strlen($tag) : -1 ); } return $return; } function parseNest( $opening, $closing ) { // Placeholder $pairs = array(); // Loop through $opening backwards foreach( array_reverse($opening) as $oVal ) { /***** THIS IS THE CHUNK I THINK CAN BE IMPROVED ON *****/ // Loop through $closing until we find a value less than the current $opening foreach( $closing as $cKey => $cVal ) // Check to see if the current opening is less than the current closing if( $oVal < $cVal ) { // If so, add it to the pairs $pairs[] = array( $oVal, $cVal ); // Unset used value of b - prevents duplication and should speed things up unset( $closing[$cKey] ); // Break out of the loop break; } /***** END OF CHUNK *****/ } // Return them in ascending order return array_reverse( $pairs ); } ?></pre>
  8. Zegan, I'm not exactly sure what you're doing... but your solution seems, well, overly complex? Also, you should never use for ($i=0; $i <count($a); $i++) Unless you expect count($a) to change. You should instead use for ($i=0, $c=count($a); $i < $c; ++$i)
  9. Okay, let me expand on this. It's a function to match up nested pairs of strings. Array $a would be the offsets for opening tags, while array $b would be the offsets of closing tags. bobbinsbro : The arrays will/should always be in ascending order... and if it wasn't, I can always use sort() before looping. sasa: Yes, they should both be in ascending order at all times. corbin: It's part of the algorithm. In order to find the correct matching pairs, it takes the least amount of work to start from the opening end ( deepest element ) and work your way up/out. Just to clear things up, here's what I'm doing, in a wider scope. This is a crude example... the actual code I'm using is nested in a class with a ton of erroneous garbage so I shortened it up [edit] oops, gotta clean a few things up before i can provide an example [/edit] Thanks for your replies thus far
  10. Well, if you want to follow PHP's function naming spec. '/[a-z_\x7f-\xff][a-z0-9_\x7f-\xff]++\([^)]++\)/i' This is assuming you won't use literal strings with closing brackets in your function... for example bleh('Stuff with a ) closing bracket') If you want something like that, the expression becomes quite a bit more complex
  11. First I'll try to explain the problem. I have 2 arrays of identical size, similar to the example below $a = array( 2, 29, 54, 77 ); $b = array( 66, 72, 102, 107 ); I want to go through each value of $a in reverse, and find the lowest value of $b that is greater than the current value of $a. To add to this mess, I don't want to reuse any values of $b when pairing them with values from $a Here's what I've got so far <pre><?php $a = array( 2, 29, 54, 77 ); $b = array( 66, 72, 102, 107 ); // Placeholder for found pairs $pairs = array(); // Loop through $a backwards foreach( array_reverse($a) as $aVal ) { /***** THIS IS THE CHUNK I THINK CAN BE IMPROVED ON *****/ // Loop through $b until we find a value less than the current $a foreach( $b as $bKey => $bVal ) // Check to see if the current value of b is less than the current value of a if( $aVal < $bVal ) { // If so, add it to the pairs $pairs[] = array( $aVal, $bVal ); // Unset used value of b - prevents duplication and should speed things up unset( $b[$bKey] ); // Break out of the loop break; } /***** END OF CHUNK *****/ } print_r( $pairs ); ?></pre> The output should look like this Array ( [0] => Array ( [0] => 77 [1] => 102 ) [1] => Array ( [0] => 54 [1] => 66 ) [2] => Array ( [0] => 29 [1] => 72 ) [3] => Array ( [0] => 2 [1] => 107 ) ) Any help is greatly appreciated. I'm hoping there's some cool array trick I just don't know about. Thanks for lookin'
  12. I believe underscore is already matched in the \w shorthand so you could make it even shorter
  13. This regex should perform the same thing in less steps than the above example preg_match_all('%<a target[^>]*+>([^<]++)</a>%', $subject, $result, PREG_PATTERN_ORDER); print_r($result[1]);
  14. Don't trust mimetype - It's defined by the client. Instead, check the extension and if you have the extension, check the _real_ mimetype using http://php.net/fileinfo
  15. I would do it like this <pre><?php $person = array("frank_jones","jim_smith","sandy_mitchell"); $folder = array("007","002","010"); $name = array("Frank","Jim","Sandy"); for( $i = 0, $c = count($person); $i < $c; $i++ ) { echo "Person: {$person[$i]} \n"; echo "Folder: {$folder[$i]} \n"; echo "Name: {$name[$i]} \n"; echo "\n"; } ?></pre>
  16. I'd do something like this: <?php $code = <<<CODE some content goes here <?php morePHPcode(); while ( !($succeed = try()) ); echo 'foo'.\$bar; ?> here's some <b>more</b> content, ect. ect. CODE; echo hiliteCode( $code ); function hiliteCode( $input ) { $regex = '/<\?.*?\?>/s'; return preg_replace_callback( $regex, create_function( '$m', 'return "<pre>".highlight_string( $m[0], TRUE )."</pre>";' ), $input ); } ?>
  17. I think i see what you mean... try this guy <pre><?php $subject = 'The cat sat on the [[straw|mat]]. The cat hoped he would jump through the [[windows]] and catch the [[feathered variety|bird on the branch]].'; $array = getValues( $subject ); print_r( $array ); function getValues( $input ) { $regex = '/\[\[([^|\]]++)(?:\|([^\]]++)){0,1}/'; if( preg_match_all($regex, $input, $matches, PREG_SET_ORDER) < 1 ) return array(); $return = array(); foreach( $matches as $m ) { if( isset($m[2]) === FALSE ) $return[] = $m[1]; else $return[] = $m[2]; } return $return; } ?></pre>
  18. Type casting or ctype_digit() would be better than is_numeric()
  19. This would be pretty tricky to do. Why on earth do you need the SFN?
  20. print_r( $_REQUEST['cmd'] ); Images return an array of the x,y coordinates that were clicked.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.