-
Posts
1,832 -
Joined
-
Last visited
-
Days Won
3
Everything posted by salathe
-
[ot]Is there any way not to mung the post's HTML when someone pastes a print_r output without code tags? It seems to happen fairly often resulting in problem <ul>s. "Remind the OP to use code tags" is of course an option too. [/ot]
-
Is this some homework assignment, to demonstrate an understanding of the algorithm? [ot]Why do the two posts from whom I assume to be the OP from different usernames?[/ot]
-
What's wrong with: list($firstName, $lastName) = explode(…);
-
What would you like to brute force?
-
Try it and see.
-
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.
-
What about IFNULL(blah, 'foo') AS foolah or some other "column" containing commas?
-
Is there any need to cater for AS (foo AS bar) or computed columns (COUNT(foo) AS bar)?
-
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.
-
Are you using an older version of PHP (say, less than 5.2.4)?
-
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.
-
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
-
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.
-
Apologies for not replying to this yesterday, have you progressed at all towards a solution or still need a hand with it?
-
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.
-
[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.
-
check if a stored time() is more than 2 hours ago
salathe replied to Vivid Lust's topic in PHP Coding Help
You could use strtotime to generate the timestamp 2 hours ago. -
And the option values?
-
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?
-
If you could show the XML and the expected HTML, any solutions for you would be easier to arrive at.
-
counting specific words in string (reliably)
salathe replied to gerkintrigg's topic in PHP Coding Help
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'."; -
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.
-
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.
-
Given the array in your print_r-like example, what would the select box look like?
-
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.