Jump to content

salathe

Staff Alumni
  • Posts

    1,832
  • Joined

  • Last visited

  • Days Won

    3

Everything posted by salathe

  1. The separate variables should be passed by reference into the array (currently, they're passed by value only). // ... $fieldVals = array(&$name, &$email, &$subject, &$message); // ... Another alternative would be to use the compact/extract functions. // ... $fieldVals = compact('name', 'email', 'subject', 'message'); foreach($fieldVals as $key => $val) $fieldVals[$key] = "abc"; extract($fieldVals); // ... P.S. Mark beat me to it, grrr it takes far too long for me to type replies.
  2. Look into DOMDocument::getElementsByTagNameNS (note the NS at the end) since you're trying to access nodes with a particular namespace (media). $nodes = $feeditem->getElementsByTagNameNS('*', 'file'); (Example uses special value "*" because I don't know the media namespace URI)
  3. Sure, perhaps the function fnmatch might be of use. For example: if (fnmatch('*/index.php', $_SERVER['REQUEST_URI'])) { echo "URL index.php"; }
  4. It might make more sense to play with mktime in this particular case as I don't think there is a simple way to get the last day of the previous month with strtotime. An example using mktime is: // Show the last day of last month $last = mktime(0, 0, 0, date('n'), 0); echo "Last day of last month is: " . date("D, j M Y", $last) . "\n"; // Last day of last month is: Wed, 30 Sep 2009 Just to run that through some quick tests: // Some tests, one for each month of this year $months = explode("|", "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"); foreach ($months as $month) { $day = rand(1, 28); $today = strtotime("$day $month 2009"); echo "\nFor " . date("d M y", $today) . ", LDOLM is "; echo date("d M y", mktime(0, 0, 0, date('n', $today), 0)); } /* Test dates are random but should be something like the following: For 22 Jan 09, LDOLM is 31 Dec 08 For 10 Feb 09, LDOLM is 31 Jan 09 For 27 Mar 09, LDOLM is 28 Feb 09 For 16 Apr 09, LDOLM is 31 Mar 09 For 14 May 09, LDOLM is 30 Apr 09 For 13 Jun 09, LDOLM is 31 May 09 For 09 Jul 09, LDOLM is 30 Jun 09 For 15 Aug 09, LDOLM is 31 Jul 09 For 22 Sep 09, LDOLM is 31 Aug 09 For 25 Oct 09, LDOLM is 30 Sep 09 For 15 Nov 09, LDOLM is 31 Oct 09 For 17 Dec 09, LDOLM is 30 Nov 09 */
  5. If you have any suggestions with regards to making that page more readable then please PM me and I'll be happy to have a chat about it with you (and suggestions might very well make it into the manual). I'm aware that the manual can be a little too technically written with lots of jargon, so any changes to make it more understandable are welcome.
  6. Alrighty, just so long as people aren't getting riled by the smarty-pants jumping on already solved threads. There's a page in the PHP manual for the ^ and $ metacharacters: http://php.net/regexp.reference.circudollar (I'd also heartily recommend that entire section on "Pattern Syntax" as something to digest over some time).
  7. The OP says that a minimum value would be $00.00 so it doesn't look like the dollar symbol nor decimal values are optional. If that's the case, /^\$\d{2,4}\.\d{2}$/D will suffice. Offtopic: cags, your pattern is ok but has a couple of problems in this case. As above, it does not look like the dollar nor decimals are optional which your regex says they are. The two main points I wanted to whisper across are that a) the dot is not escaped so will match any non-newline character (including a dot), b) you use the dollar anchor quite rightly, but it can allow a trailing newline in the subject string unless you use the D modifier: e.g. the string "$1234567\n" is allowed. P.S. I feel terrible commenting on peoples' solutions (and all the time after the threads have been solved!) when all they are trying to do is be helpful. If my two cents that I keep throwing around are starting to sting, do let me know! :-\
  8. It looks like you're after Output Buffering, in particular the ob_start and ob_get_clean functions.
  9. Given the input string "(S2O3)-2", what do you want to be captured (ie, what should/would the matches array look like) exactly?
  10. Following on from cags's post, strtotime is your friend but it can be done much simpler using the phrase previous weekday. As usual with strtotime, if you want to make it relative to a specific date (defaults to today) then just provide the timestamp as a second argument. Magic $last_weekday = strtotime("previous weekday"); echo "Previous weekday was " . date("D d M"); Just as a quick test, try: foreach (range(1,21) as $dom) { $today = strtotime("$dom Oct 2009"); $wkday = strtotime("previous weekday", $today); echo "Previous weekday for " . date("D d M", $today) . " is " . date("D d M\n", $wkday); }
  11. As with others, I'm fine with my gmail address being caught since the spam filters work really well in Gmail. The email address in my signature uses the Mailhide service to save that particular address from getting too much spam.
  12. $decimal = unpack('V', $binary);
  13. Hi there, if you would click on the "Report a bug" link on that page and fill out a documentation bug report then the documentation team will be made aware of the omission. If you could do that, it would be great. By default, simplexml_load_file creates (a heirarchy of) SimpleXMLElement objects and you can use the SimpelXMLElement::children method to access namespaced child nodes. A quick example would be: $xml = simplexml_load_file('http://earthquake.usgs.gov/eqcenter/catalogs/eqs7day-M5.xml'); foreach ($xml->channel->item as $item) { $geo = $item->children('geo', TRUE); echo "At " . $geo->lat . "," . $geo->long . " was " . $item->title . ".\n"; }
  14. If you didn't want to, or couldn't, upgrade PHP then the sha1 and md5 functions should be available in your version of PHP.
  15. With SimpleXML, one can access attributes using array notation. See this quick example: $xml = new SimpleXMLElement('http://dblp.uni-trier.de/search/author?xauthor=smith', NULL, TRUE); // Get first author's URL pointer $urlpt = (string) $xml->author['urlpt']; echo $urlpt;
  16. Give a more specific example of the XML that you want to work with, and what you want to get out of it. SimpleXML can access attribute values (href is an attribute, www... its value) just as easily as "the data between tags".
  17. Yes. What attributes are you looking to ead?
  18. There is nothing, syntactically or otherwise, wrong with your regular expression which would prevent it from matching what you want it to match. (Aside: cags mentions that equals is not a special character so does not need escaping with a backslash. This is true, but if one does put a backslash before non-special characters it will just be ignored anyway.) The regex will happily (if an expression can have emotion) match occurrences of type="hidden" in a string. If this is returning a zero count then there may be no occurrences of that exact string present. As a quick example using your exact regular expression: $subject = 'type="hidden"type="hidden"type="hidden"'; $count = preg_match_all("/type\=\"hidden\"/", $subject, $matches); var_dump($count); // int(3) Another alternative, because your example does not really require the use of a regular expression (simple string matching will suffice) and only wants the count, would be to call on the handy substr_count function: $subject = 'type="hidden"type="hidden"type="hidden"'; $count = substr_count($subject, 'type="hidden"'); var_dump($count); // int(3)
  19. No, you don't need to use cURL. You can, but it is not required. Exactly what you need to do depends on the auth systems in place on the remote website; often you'll need to log in, saving the cookie that is returned on successful login, then access the protected page sending over that cookie. If you could give some more details of the procedure you would normally go through to browse to the page in question, we would be able to give you help with the finer details of what to do.
  20. You are right, they are measured from the top left. Increasing values for $cx go from left to right, increasing values of $cy go from top to bottom.
  21. (Tangent/)Offtopic: @Crayon Violent: It is a double negative, but your explanation is not correct. In the code described, ereg will return 1 (i.e, true/found) if there is any single non-digit character. Rather than returning true "if there are no numbers in it", it will return true if there is one (or more) non-digit in the string. The "logical not" operator (!) is then used to flip the true/false so the full condition of the if statement will be true if there is not at least one non-digit in the string (ie, true if the value only contains digits). In other words true for 1234567 and false for 12345a.
  22. Variable names are case-sensitive. In the line with array_push, you use the variable $OrderofOperations whereas elsewhere you use $OrderOfOperations - note the capitalisation of "Of".
  23. You could also do something like: var subject = " Hello World "; var trimmed = subject.trim().replace(/ +/g, ' ');
  24. It seems a strange thing to need to do! One quick way would be: $var = array( 'name' => 'thename', 'title' => 'thetitle', 'media' => 'themedia' ); // Remove first element (the name) $name = array_shift($var); // Add it on to the end $var['name'] = $name; var_dump($var); /* array(3) { ["title"]=> string( "thetitle" ["media"]=> string( "themedia" ["name"]=> string(7) "thename" } */
  25. A quick solution would be to replace all of the escape sequences present in the code (\123 is an escape sequence; the chances of finding one in PHP code outside of a string are minimal). It is worth noting here that escape sequences like those presented in this thread represent ASCII characters within double-quoted strings and can use either octal or hexadecimal notation. (For more info, peek at the Double quoted strings in the PHP Manual.) The octal notation of a character can be represented by the regular expression \[0-7]{1,3} (Aside: For now, assume the backslash means a literal backslash character.), in other words a backslash followed by between one and three digits between one and seven (since octal numbers are base 8, those are the only digits used). The hexadecimal notation of a character can be represented by the regular expression \x[0-9A-Fa-f]{1,2} In other words, a backslash and letter x followed by one or two hexadecimal digits. We can put these two together to help solve our problem which can be divided into two main steps: [o]Match our (octal or hexadecimal) escape sequences [o]Replace them with their ASCII characters The first step requires us to figure out a regular expression which will match the octal/hex escape sequences. Luckily, here's one that I prepared earlier so we will just use it (if you need an explanation, just ask). The Regex /\\([0-7]{1,3}|x[0-9A-Fa-f]{1,2})/ We will eventually need three backslashes due to the PHP parser thinking that the slash might be the start of an escape sequence (like the PHP code we're going to be replacing!) and the regular expression engine using that same character for its own escape sequences (the latter is why we need two in the example given)! The details aren't super-important suffice to say that you will need all three there in your PHP code (as shown below). Now, the replacement. Replacement function The easiest way to do our replacements is to use a callback function (using preg_replace_callback), which takes in the matched values, examines them, and returns the value we want in their place. The idea is fairly simple; the matched values (our octal or hexadecimal escape sequences) will be fed into the function, we convert that to the appropriate ASCII character and return that character. The callback function is called once for each escape sequence found. This function could be written in a myriad of different ways to get the job done, this is just one quick example. function decode_octhex($match) { // E.g 166 or x6f $value = $match[1]; // Hexadecimal notation if ($value[0] == 'x') { return chr(hexdec(ltrim($value, 'x'))); } // Octal notation return chr(octdec($value)); } Now it's all well and good having the component parts, but for the sake of simplicity (and what I know you're really here for) lets put all of the pieces into a small script so that we can tie everything neatly together. Example Script <?php // Use file_get_contents() or whatever here. $encoded = '<?php $x0b = "\166\x65\156\x6f\x6f\165\1631\x5f\x6d\141\x77\x61\x6c\171"; $x0c = "ve\156\x6f\x6f\x75\163\061_m\x61waly"; $x0d = "\x33%\x28\124\124?\x3a\x7dA\102A4"; $x0e = "\x6c\157ca\x6c\x68os\x74"; $x10 = "\x35\x2d10-\x32\x301\x30"; $x11 = mysql_connect($x0e, $x0c, $x0d); ?>'; /** * Takes a hexadecimal or octal value * and returns its equivalent ASCII character. */ function decode_octhex($match) { // E.g 166 or x6f $value = $match[1]; // Hexadecimal notation if ($value[0] == 'x') { return chr(hexdec(ltrim($value, 'x'))); } // Octal notation return chr(octdec($value)); } // Replace octal/hexadecimal escape sequences with their ASCII values echo preg_replace_callback('/\\\([0-7]{1,3}|x[0-9A-Fa-f]{1,2})/', 'decode_octhex', $encoded); ?> Example Output <?php $x0b = "venoous1_mawaly"; $x0c = "venoous1_mawaly"; $x0d = "3%(TT?:}ABA4"; $x0e = "localhost"; $x10 = "5-10-2010"; $x11 = mysql_connect($x0e, $x0c, $x0d); ?> There we go!
×
×
  • 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.