Jump to content

salathe

Staff Alumni
  • Posts

    1,832
  • Joined

  • Last visited

  • Days Won

    3

Everything posted by salathe

  1. I guess I'm still in that other thread (assumed the OPs were one and the same). Well, if the OP wants to cater for those slightly more complex fields then he only has to say so.
  2. What about IFNULL(blah, 'foo') AS foolah or some other "column" containing commas?
  3. Is there any need to cater for AS (foo AS bar) or computed columns (COUNT(foo) AS bar)?
  4. The filesize function expects it's parameter to be a string containing the path to the file. In your script $file is a resource, not a string. Perhaps you instead want $sizeOfFile = filesize($localPath); You could also get the information from cURL to see how big the downloaded file is.
  5. Are you using an older version of PHP (say, less than 5.2.4)?
  6. The path needs to be a path to an actual file on disk, not an URL, for cURL to find and pass along with the request. lococobra, unless the information is particularly sensitive it would be advisable to post your solution in public.
  7. The data for buoy ID 46005 (Washington) does not appear to exist. Because of that the NDBC returns a standard 404 HTML error page which your script tries to parse as valid data: http://www.ndbc.noaa.gov/data/realtime2/46005.txt
  8. Pending the answer to cags' post, it might also be fine to use something like /{(.+?)}/ (you'd then need to loop over $matches[1]). There are a tonne of varieties of regular expression which might be useful, better, faster, easier, etc. and as usual there's no "one right answer". If you do need to cater for nested values, then both of our replies will need some rethinking.
  9. Apologies for not replying to this yesterday, have you progressed at all towards a solution or still need a hand with it?
  10. Please always make sure that you have the error reporting level cranked up (e.g., error_reporting(E_ALL);) and that PHP errors are displayed (e.g., ini_set("display_errors", "1");) when writing your code. It would have caught this problem for you with a helpful error message.
  11. [ot]For the lazy and/or time-short folks, scroll down to the first code block. That's all that's important. With AlexWD's post, even the code isn't important… serves me right for taking over half an hour to format a post (amongst doing other things).[/ot] I'm not sure why you would need this to be recursive (as shown by your code attempt, but not in your description) so I'm ignoring that for the moment. The tasks that we need to accomplish follow the general sequence of: 1. Find the parts of the string (the file contents) that we want to deal with. 2. Extract the Base64 encoded values from those parts. 3. Decode those encoded values. 4. Replace the whole part with the decoded value wrapped in quotes (since we're writing PHP code). Number 1 is easy, we all know that regular expressions can do anything[1]. This counts as anything, so lets use them. We need an expression to match base64_decode('gobbledygook') where the gobbledygook is a Base64 encoded value. Matching the literal parts is easy, matching the Base64 encoded value is also easy since we can just say to match the characters available for Base64 (a-z, A-Z, 0-9, +, / and some trailing = characters). I'm going to assume a working knowledge of regex, so won't delve too deeply into it (in fact, I won't delve at all into it; just present you with the code). The part of regular expression matching the Base64 encoded value is simplified in my example below but since we're replacing existing (hopefully working) PHP code we shouldn't have to worry about things which look like Base64 encoded values yet aren't. Number 2 is also easy with regex, we just use a capturing group ((…)) around the Base64 encoded value. For number 3 we have the super-convenient base64_decode function available to change the encoded value into a decoded one. For number 4, we'll use a function in conjunction with preg_replace_callback to take the entire match and replace it with what we want. The code should really explain itself (it's only 4 lines!). To put it all into code we simply grab the file contents and perform a regex replacement doing all that stuff described above. This example just echoes out the "decoded" file contents, you can do whatever with it (write it back to a file, perhaps). <?php $encoded = file_get_contents('encoded.php'); function callback($match) { return "'" . base64_decode($match[1]) . "'"; } echo preg_replace_callback("~base64_decode\('([a-zA-Z0-9+/]+={0,3})'\)~", 'callback', $encoded); ?> Given the file encoded.php with the following contents: <?php $var = "test file"; $var2 = base64_decode('ZXhhbXBsZQ=='); $test = array(base64_decode('ZXhwZXJpbWVudA==') => 'example'); ?> The output from the example script is something like: <?php $var = "test file"; $var2 = 'example'; $test = array('experiment' => 'example'); ?> -- [1] Yes, literally anything.
  12. You could use strtotime to generate the timestamp 2 hours ago.
  13. Having example input and output would help us to see what kind of translation you're looking for given specific structures of the XML. To clarify, there are different ways to translate the following XML: <example> <a>apple</a> <a>orange</a> <b> <c>cherry</c> <d>damson</d> </b> <e>elderberry</e> </example> Given that (would you need to even cater for structures like that?) what would the HTML be?
  14. If you could show the XML and the expected HTML, any solutions for you would be easier to arrive at.
  15. Given the $string and $word values, a basic method of counting occurrences of that word is like: $string = 'websites on the web are cobwebs'; $word = 'web'; $word_escaped = preg_quote($word, '~'); $pattern = '~\b' . $word_escaped . '\b~'; $count = preg_match_all($pattern, $string, $matches); echo "'$word' occurs $count time(s) in '$string'.";
  16. The code may be different but the problem is always the same: counting (or matching) words in a string. Thank you for updating one of the other threads with where you are at now.
  17. Neil, your other threads on this topic provide answers (or the best answer we can give based on your input). Please take the time to go back over them. If you're still stuck, please reply there with specific comments on what is/is not working, rather than create dozens of new threads.
  18. Given the array in your print_r-like example, what would the select box look like?
  19. I would also like to echo David's sentiments. From someone who tends to answer more than ask, I'd like to thank the staff and the community for making it what it is and for making it a nice place to come and help or be helped.
  20. First, you're using the wrong type of quotation marks (double instead of single) in that snippet. Second, it is advisable to use preg_quote because the variable value might contain special characters (like yours contains the dot) which mean something in regular expressions. An example might be: $img = "fun.jpg"; preg_match_all('~[\'"](.*?)' . preg_quote($img, '~') . '[\'"]~is', $str, $a); I sometimes like to make things a little "neater" (arguably) using sprintf but it's purely cosmetic and does nothing different than the code above: $img = "fun.jpg"; $pattern = sprintf('~[\'"](.*?)%s[\'"]~is', preg_quote($img, '~')); preg_match_all($pattern, $str, $a);
  21. Perhaps this function may help, it looks like it might.
  22. Is it literally that text? I only ask because it's an odd format to provide data in (for consumption).
  23. You could also make use of the SPL, in particular the SplFileObject to get at the colon-separated-data in an object-oriented way. Here's an example which should (in theory) be pretty straightforward to understand. $users = new SplFileObject('users.txt'); $classes = new SplFileObject('class.txt'); foreach (array($users, $classes) as $file) { $file->setFlags(SplFileObject::READ_CSV); $file->setCsvControl(':'); $file->headings = $file->current(); } // Get a list of students foreach ($users as $user) { $user = array_combine($users->headings, $user); // Collect only students if ($user['Status'] === '0') { $students[$user['Email']] = $user; } } // Iterate over classes foreach ($classes as $class) { $class = array_combine($classes->headings, $class); // Remove from students list if (isset($students[$class['Email']])) { unset($students[$class['Email']]); } } print_r($students);
×
×
  • 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.