Jump to content

salathe

Staff Alumni
  • Posts

    1,832
  • Joined

  • Last visited

  • Days Won

    3

Everything posted by salathe

  1. Here are three more variations on the theme. They all take the variable $str and chomp off the "CATEGORY: " prefix if there is one. The first two require the prefix to be at the start of the string. $str = "CATEGORY: ITEM"; // If prefix exists for string, remove it. $prefix = "CATEGORY: "; if (strpos($str, $prefix) === 0) { $out = substr($str, strlen($prefix)); } // Same as above $out = preg_replace("/^CATEGORY: /", "", $str); // (Almost) same as above $out = str_replace("CATEGORY: ", "", $str);
  2. You can check whether there is an Error node by using isset, for example, isset($xml->Address[0]->Error)
  3. You're reading STDERR (usually for errors), do you instead want to be reading STDIO (SSH2_STREAM_STDIO)?
  4. Just because it's helpful to know terminology (e.g. to aid further research): @ is the error control operator (or error suppression operator, manual) ?: is the ternary operator (manual)
  5. How about placing "SOLVED by xyz, abc" in the title attribute of the link so you can mouse over and see it? Here's a mockup of what it would look like if names were added into the thread title prefix:
  6. $validationOK = FALSE; if (trim($Human_check_1) == "2" OR strtolower(trim($Human_check_1)) == "two") { $validationOK = TRUE; } Reading: 1. Logical operators (the OR) 2. strtolower function
  7. You can call stream_get_contents on the stream resource, $output.
  8. Take a look at the following example and see which of the array functions best suits your needs. <?php $a[1] = 1; $a[2] = 1; $a[3] = 1; $a[4] = 1; $b[3] = 1; $b[4] = 10; // Diff by key only print_r(array_diff_key($a, $b)); // Diff by key and value print_r(array_diff_assoc($a, $b)); ?> Array ( [1] => 1 [2] => 1 ) Array ( [1] => 1 [2] => 1 [4] => 1 )
  9. The following example script: <?php $json = file_get_contents('http://letsbetrends.com/api/current_trends'); $data = json_decode($json); $topics = array(); foreach ($data->trends as $trend) { $topics[] = $trend->name; } print_r($topics); Gives output like: Array ( [0] => Halloween [1] => Windows 7 [2] => Nick Griffin [3] => TGIF [4] => Paranormal Activity [5] => Follow Friday [6] => #iusuallylieabout [7] => Soupy Sales [8] => Cassetteboy [9] => #mylasttweetonearth ) $topics is an array of the trending topics, which I think is what you wanted.
  10. Perhaps the HEREDOC syntax might be of use? echo <<<HEREDOC <b><a href="javascript:animate.show(['why', 'how1', 'how2'])"><img src="show.jpeg" border="0" /></a></b> HEREDOC;
  11. Remember to send out a 503 Service Unavailable header with an appropriate Retry-After time.
  12. No problem. Remember to click the "solved" button to mark the thread as solved.
  13. Assuming your array (as shown in the first post) is in $list then the code can be simpler than I think you think it should be. $fp = fopen('imported' . date('mdyHis') . '.csv', 'w'); foreach ($list as $row) { fputcsv($fp, $row); } fclose($fp); P.S. that gives the output (to the file): 123,abc,555-555-5555,,,,abc@123.com 456,def,,,,,def@123.com P.P.S. You could add the headers by doing the following immediately before the foreach loop: fputcsv($fp, array_keys($list[0]));
  14. No shreds will be ripped in this post. There is no real reason to, but it's generally nice to, be as explicit as possible with what you want to match (and you can learn what the new bits mean!). To make sure you only grab the anonymous seller email, perhaps extend the expression to look for the pattern the emails share: /mailto:(sale-(?:[a-z0-9]{5}-)?\d{9,10}@craigslist.org)/ That'll give you something to sink your teeth into. For help, check out Regular Expression Quick Start and go from there. If that's far too over-your-head then how about /mailto:(sale-[^@]+@craigslist.org)/ as a nice intermediate?
  15. Maybe I'm missing something obvious but shouldn't you be using just $line where you currently use split(',', $line)?
  16. Or ctype_digit, or filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 4))) for a slightly (ahem) more verbose option.
  17. Redirect them to a page without that value in the URL.
  18. I'm not sure there is any advantage (unless you can include confusing yourself or whoever else looks at the pattern in the future as an advantage). You can use a pair curly braces or a pair of (), [], or <>. Aside from being just-another-thing-to-know, and the fact that you can draw amusing pattern-faces (e.g. <(o.o)>) I don't see any benefit.
  19. The question and description are unclear, what exactly is the problem? However, I would offer one change to how you construct what I assume should really be an array of words and their translations. while($row = mysql_fetch_array( $result )) { $words[] = $row['word']; $filter[] = $row['translated_word']; } echo str_replace($words, $filter, $str);
  20. Absolutely, doing that would indeed suppress any parsing warnings from being displayed. My main reason for recommending libxml_use_internal_errors (apart from it being IMO "correct") is because the @ operator will do more than you want. What you want is to keep HTML parsing errors from being a nuisance, but what you get is all* errors bring kept quiet. If you mistype the method name, there will be no fatal error; mistype the variable name, no notice; if the filename argument is empty (or points to an empty file) no warning will be raised. Those problems will lead to unexpected behaviour of your script and all manner of troublesome bugs. By all means, use it as a super-quick and easy way of silencing those pesky HTML errors but do be aware of the caveats of doing so. * Parse errors will still be thrown.
  21. From one call to rand(), no.
  22. To OP: Do you really want to round to the nearest 5/100ths? If so, your numbers are incorrect, 1.26 is closer to 1.25 than 1.30. Perhaps you want to round up to the next 5/100ths? If that is the case, look to JAY6390's post. (Edit: added "To OP" prefix)
  23. The regular expression is catching more than just the URL. There are three different times for each file where the URL is located in the HTML code (two in <a> tags, one plain text). The plain text links have a trailing line break when the regex matches them, making that different to the ones from <a> tags for any given file. Adjust your regex (it's a simple fix) so that only the URL itself is matched and the three links for a file will be the same and array_unique will give you what you expect.
  24. Do you have one row with many times, or many rows with one time in each? What is the column type being used (TIME, DATETIME...)?
  25. I'm not really sure if this reply will go way over the OP's head or not, hopefully not. Using the techniques displayed in this thread (parsing with the DOM, extracting nodes with XPath) it would be easy to only target the specific anchor nodes that you really want. Removing duplicates because of a too-wide search parameter would not be an issue at all in that case. XPath The first step is to find out exactly which anchors you will be wanting to pull out of the HTML. Here's a few traits common only to those that we want: href attribute starts with "http://rapidshare.com/files" anchor is an immediate child of a td tag Converting that into an XPath expression is pretty straight-forward if you know the basics of what it should look like, if not it might be a bit of a puzzle and definitely something you should read up on. Translating the above into an XPath expression gives: //td/a[starts-with(@href, "http://rapidshare.com/files")] (Note use of starts-with function rather than contains) Personally, I'd go for an even more specific expression but lets try and keep things simple here. Error Handling Also, rather than using the error suppression operator (@) on the line which calls DOMDocument::loadHTMLFile, I would advise instead using the libxml's error handling functions to silence, capture, or do whatever you like with any errors that may be raised when parsing the HTML document. This leaves any other errors in that line of code (like typos, etc.) to act as normal. Here's a quick example script based on the code already posted in this thread and those changes mentioned above. Example <?php $dom = new DOMDocument; // Use our own error handling for libxml (we'll just be ignoring warnings) libxml_use_internal_errors(TRUE); // Load RS link list (note: this is not full of porn) $dom->loadHTMLFile('http://rapidshare.com/users/SI4XAY'); // Turn off our own error handling libxml_use_internal_errors(FALSE); $xpath = new DOMXPath($dom); $links = $xpath->query('//td/a[starts-with(@href, "http://rapidshare.com/files")]'); foreach ($links as $link) { echo $link->getAttribute("href") . "\n"; } ?> Which outputs (or, should output) something like: http://rapidshare.com/files/296051541/b.txt http://rapidshare.com/files/296051540/a.txt Useful Links libxml_use_internal_errors function XPath - An overview of XPath by Tobias Schlitt and Jakob Westhoff (pdf)
×
×
  • 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.