-
Posts
15,229 -
Joined
-
Last visited
-
Days Won
427
Everything posted by requinix
-
parse_url to get the query string and parse_str to get it as an associative array.
-
How would you remove the index.php when visiting a url?
requinix replied to helloworld001's topic in PHP Coding Help
Don't put "index.php" in your links. If the file doesn't load automatically then your web server doesn't know that it can treat index.php as an "index" file in a directory. Or are you talking about redirecting from /index.php to /? -
You can't mix PHP and Javascript like that. $w is literally the string "". Normally I would reply with something like "You can do the width
-
file_get_contents and curl not working for some url
requinix replied to Reneb's topic in PHP Coding Help
It may very well be firewalled on the host's side. If they have shell access they can try to troubleshoot some more, but without that it's basically just guessing. Try other hosts and other ports and see what works. -
file_get_contents and curl not working for some url
requinix replied to Reneb's topic in PHP Coding Help
Some people can't use it? It could totally be an issue with their host. Especially if they can't get other things. It's not like TARGETHOST is some local server, right? It's something sitting out there in the internet that anybody should be able to access without any firewalls or LANs to get in the way, right? -
Your current regex will also accept "$00.00/€00.00". Sounds like it has to be $/$ USD or €/€ EUR? Go for the most obvious answer: #\((\$\d+\.\d+/\$\d+\.\d+ USD|€\d+\.\d+/€\d+\.\d+ EUR)\)#But you're dealing with non-ASCII characters so there's a little bit more work to do.1. Use Unicode in the regex, including an escape sequence for the Euro sign (or else it will depend on your file encoding). '€' is U+20AC. The /u flag is required and enables UTF-8 mode. '#\((\$\d+\.\d+/\$\d+\.\d+ USD|\x{20AC}\d+\.\d+/\x{20AC}\d+\.\d+ EUR)\)#u'2. Make sure your $haystack is in UTF-8. $euro = "\xe2\x82\xac"; // utf-8 encoding of \x{20AC} $haystack = array( '($00.00/$00.00 USD)', // match '($00.00/' . $euro . '00.00 USD)', // no match '(' . $euro . '00.00/$00.00 EUR)', // no match '(' . $euro . '00.00/' . $euro . '00.00 EUR)' // match ); $regex = '#\((\$\d+\.\d+/\$\d+\.\d+ USD|\x{20AC}\d+\.\d+/\x{20AC}\d+\.\d+ EUR)\)#u'; foreach ($haystack as $h) { var_dump(preg_match($regex, $h)); }
-
No, that is not supported. If you want an array then just use a loop, it's hardly any more work.
-
Maybe you're looking for "alternation"? if(preg_match('/java|PHP|html/', $text, $match)) { echo "Found ", $match[0]; // 0 is the entire string that was matched, so "java" or "PHP" or "html"The pipe separates different parts of the regex that can match. A|(B|C)D matches "A" or "BD" or "CD".
-
If you were to remove the existing "good" certificate and put in a self-signed "bad" certificate, yes. You can't actually remove SSL and have https work. Nope. Can't have https without SSL. Users have to be able to connect to the server, send a request, and receive the response which tells them to go to new-domain.
-
Multi-threading is one thing PHP can't really do and that you can have a lot of fun with. I know C# can do it but I don't know if/how other languages can.
-
That is horrible. Short echo tags to the rescue! <?='T'?><?='h'?><?='a'?><?='n'?><?='k'?><?=' '?><?='y'?><?='o'?><?='u'?><?=','?><?=' '?><?='P'?><?='H'?><?='P'?>
-
real time chat databse output update?
requinix replied to moose-en-a-gant's topic in PHP Coding Help
I wouldn't call it the "normal solution" but it is cheap and easy to do. A good start. Yup. You'll find all this a lot easier to do with a Javascript library like jQuery. Actually it's quite a reasonable idea, but it comes with a problem: how do you tell the connected users that they need to refresh? And if you can tell them to refresh, why not just tell them about the new post instead? -
Recommend turning errors into exceptions?
requinix replied to NotionCommotion's topic in PHP Coding Help
Can't catch those. You can often react after-the-fact but that's a different discussion. All fatal and uncatchable. If you're talking about PDOException, that has nothing to do with an error handler. Or PDO may return a silent false on error. No PHP warnings. For other functions, say fopen() on an unreadable file, or PDO problems with ERRMODE_WARNING, theoretically you could use an error handler to convert that into a FileNotFoundException (or whatever) but that would be impractical as it requires parsing the assorted error messages. Which is not nearly worth the effort. A generic ErrorException is the only option, but it's worthless in normal use: throwing it implies you try to catch it somewhere up the call stack, but you have no way to differentiate between an unreadable file or one of the thousands of other potential warnings PHP can raise. Thus catching it means you're using the "catch everything" approach to exceptions, which is a very bad practice to get into. With the current state of PHP, warnings are worth more as warnings than as exceptions. An exception and an error are basically of the same status: a fatal error of some sort. That may be totally fatal like your script stops entirely, or that may be "relatively fatal" in that the code that was executing at the time has to stop but other code may be able to continue. For example, a FileNotFoundException thrown when opening a file is relatively fatal because the file could not be opened - the whole point of the "opening a file" code - but calling code may catch the exception and deal with the problem. Like present a message to the user. PHP doesn't have a "relatively fatal" error, so it uses warnings and false/null/error codes instead. -
real time chat databse output update?
requinix replied to moose-en-a-gant's topic in PHP Coding Help
A "refresh rate" sucks, but is easy to implement. If the chat is slow, like a message every few seconds, then that might work for you. You actually do it with Javascript/AJAX so there's no literal refreshing. More modern is WebSockets: you establish a dedicated, persistent connection between you and the client's browser and the two of you can trade information however you see fit. Couple downsides though. A persistent connection ties up resources on your end and can cause problems, especially on a "limited" or otherwise shared hosting server. It's also a bit harder to implement since it's not built into most web servers and PHP. Before WebSockets was Flash and Java, which do basically the same thing (but only for the client half). -
Recommend turning errors into exceptions?
requinix replied to NotionCommotion's topic in PHP Coding Help
Exceptions should be thrown when the code cannot possibly continue what it is doing and needs to drastically fail. Except for maybe E_USER_ERROR like I said earlier, your error handler can't know whether a warning (or notice or whatever) is so bad that the code raising it cannot continue. In fact, it's more likely that such problems are not fatal because if they were then they wouldn't be mere warnings. That means the error handler should not throw exceptions. Log them, spit them out into your HTML, ignore them, whatever, but don't kill your code over them. -
You had to get that q value from somewhere, right? Database? Used some logic to piece together a URL? Or is it coming from an external source you don't control?
-
I hate using heredoc for output. There's no IDE support for HTML inside strings, to name one problem with it. So I do exactly that kind of open/close stuff - especially if I have to jump between logic and HTML a lot. Rules I follow: 1. A line with PHP code is wrapped in open and close tags. The entire line. 2. If I want a block of code, open tag + blank line + code + blank line + (if not at the end of the file) close tag 3. Open tag is always at the beginning of the line. Indentation happens right after. <?php foreach ($objects as $object) { ?> <div class="object"> <h2><?=$object->name?></h2> <?php if ($object->hasProperties()) { ?> <h3>Properties</h3> <table class="object-properties"> <?php foreach ($object->getProperties() as $name => $value) { ?> <?php switch (true) { case is_scalar($value): $printable = "(" . gettype($value) . ") " . $value; break; case is_array($value): $printable = "array(" . count($value) . ")"; break; case is_object($value): $printable = "object(" . get_class($value) . ")"; break; default: $printable = gettype($value) . ": " . @(string)$value; break; } ?> <tr> <th><?=$name?></th> <td><?=$printable?></td> </tr> <?php } ?> </table> <?php } ?> </div> <?php } ?>I also tend to have my IDE set up to color HTML differently from PHP code so there's that visual distinction, not to mention the obvious presence of a "<?php" at the beginning of the line.
-
Recommend turning errors into exceptions?
requinix replied to NotionCommotion's topic in PHP Coding Help
"Fatal" errors, yes (the only one you can really convert is E_USER_ERROR), warnings, maybe, but I wouldn't do it for notices, strict, or deprecation errors. Those aren't so severe that I want to drop what I'm doing to fix them, and more importantly I don't want my live site to crash because a rare codepath triggered an undefined index notice that I didn't encounter while testing. All that would teach you to do is try/catch everything and that's definitely worse than letting some minor issues get past you. Until PHP has exceptions in the engine, and specialized exceptions at that, I wouldn't rely on ErrorException. It's helpful to generate backtraces automatically but I don't throw them. There's a fair amount of irrelevant stuff but <?php namespace Kernel { /** * @property-read \AssertionException $assertion */ class AssertionEvent extends Event { } /** * @property-read \Exception $error */ class ErrorEvent extends Event { } final class ErrorHandler { private static $top = null; private $callback = null; private $mask = 0; private $previous = null; /** * @param callable|bool $callback * @param int $mask */ private function __construct($callback, $mask = null) { $this->callback = $callback; $this->mask = ($mask ? (int)$mask : -1); } /** * Handle an assertion * * @param string $file * @param int $line * @param string $code * @param string $description */ public static function assertion($file, $line, $code = "", $description = null) { $e = new \AssertionException($code, "Assertion" . ($description ? " '{$description}'" : $code) . " failed", $file, $line); event(new AssertionEvent(["assertion" => $e])); self::bubble($e, E_USER_ERROR) || trigger_error("{$e->getMessage()} in {$file} on line {$line}, unhandled", E_USER_ERROR); } /** * Bubble an exception through the handlers * * @param \Exception $e * @param int $severity * @return bool */ private static function bubble(\Exception $e, $severity) { for ($handler = self::$top; $handler; $handler = $handler->previous) { if (!($handler->mask & $severity)) { continue; } if (is_callable($handler->callback) ? call_user_func($handler->callback, $e, $severity) : $handler->callback) { return true; } } return false; } /** * Handle an error * * @param int $errno * @param string $errstr * @param string $errfile * @param int $errline * @return bool */ public static function error($errno, $errstr, $errfile, $errline) { if (error_reporting() == 0) { $e = new \ErrorException($errstr, 0, 0, $errfile, $errline); notice("kernel.error", new \ErrorException("Suppressed error: {$errstr}", 0, $errno, $errfile, $errline, $e)); return true; } else { $e = new \ErrorException($errstr, 0, $errno, $errfile, $errline); event(new ErrorEvent(["error" => $e])); return self::bubble($e, $errno); } } /** * Handle an uncaught exception * * @param \Exception $e */ public static function exception(\Exception $e) { event(new ErrorEvent(["error" => $e])); self::bubble($e, E_USER_ERROR); } /** * Pop the most recent error handler off of the stack */ public static function pop() { self::$top && self::$top = self::$top->previous; } /** * Push a new error handler onto the stack * * function $callback(\Exception $e, $severity) returns bool * * @param callable|bool $callback * @param int $mask * @return \Kernel\ErrorHandler */ public static function push($callback, $mask = null) { debug("kernel.error", [ "Adding %serror handler for %s", is_bool($callback) ? ($callback ? "true " : "false ") : "", ($mask ? "mask " . (int)$mask : "all errors") ]); $top = new self($callback, $mask); $top->previous = self::$top; return self::$top = $top; } } } namespace { /** * Return a single frame from a backtrace * * The default $frame = 0 will give the stack frame for the code calling your function. * * @param int $frame * @param int $options * @return array */ function debug_backtrace_frame($frame = 0, $options = null) { is_null($options) && $options = DEBUG_BACKTRACE_PROVIDE_OBJECT; foreach (debug_backtrace($options) as $stackframe) { $lastframe = $stackframe; if (isset($stackframe["file"], $stackframe["line"])) { if ($frame-- < 0) { break; } } } return $lastframe; } final class AssertionException extends Exception { protected $description = ""; /** * @param string $description * @param string $message * @param string $file * @param int $line * @param \Exception $previous */ public function __construct($description, $message = "", $file = null, $line = null, Exception $previous = null) { parent::__construct($message, 0, $previous); $this->description = $description; if ($file === null || $line === null) { $frame = debug_backtrace_frame(0, 0); $file || $file = $frame["file"]; $line || $line = $frame["line"]; } $this->file = $file; $this->line = $line; } /** * Get the code which triggered the exception * * @return string */ public function getDescription() { return $this->description; } } assert_options(ASSERT_CALLBACK, "\\Kernel\\ErrorHandler::assertion"); assert_options(ASSERT_WARNING, false); set_error_handler("\\Kernel\\ErrorHandler::error"); set_exception_handler("\\Kernel\\ErrorHandler::exception"); listen("__applyconf", function() { foreach (cfg()->node("error/handlers") as $handler) { \Kernel\ErrorHandler::push($handler->handler, $handler->mask); } }); } -
Trouble re activating account
requinix replied to cssfreakie2's topic in PHPFreaks.com Website Feedback
I've sent you a new password. -
Retaining apostrophes in email subject line
requinix replied to ohboyatoytruck's topic in PHP Coding Help
I'll rephrase: How about posting the rest of your code? -
Recommended internal encoding for PHP
requinix replied to NotionCommotion's topic in PHP Coding Help
As long as you use the same thing everywhere, and that isn't the GBK encoding (which is for Chinese so I doubt you would), you'll be fine. Latin1 (ISO 8859-1) vs UTF-8 is a question of what characters you want to support. UTF-8 would be a great choice because it supports much, much more than Latin1 does. -
You're overwriting $message every time you go through the loop. Either keep building upon $message (such as $message .=) or use it immediately.
-
preg_replace not catching '<3' as a replaceable text
requinix replied to Monkuar's topic in PHP Coding Help
There's an imaginary point in the processing where before you were dealing with raw text and after you're dealing with HTML. Right then is when you apply htmlspecialchars(). do_smilies() does deal with HTML so it is after that point. Not at the end, otherwise you'd be escaping your tags. do_smilies() or some other BBCode-type replacement is probably where you make the transition to dealing with HTML, so immediately before that would be when you apply htmlspecialchars().