[PHP] array question
Hello all! I have a question regarding arrays and the way I can use a value. Let's say I have this string: $s = 'banana,apple,mellon,grape,nut,orange' I want to explode it, and get the third value. For this I would normally do: $a = explode(',', $s); echo $s[2]; That's all fine, but is there a way to get the value directly, without having to write another line in my script. I mean something like this: echo explode(',', $s)[2]; or echo {explode(',', $s)}[2]; I couldn't find out this answer anywhere, that's why I posted here. Cheers and thanks!
Re: [PHP] array question
Tanks for all of your responses! I guess a function is the way to go. I just have to see if the situation comes up enough times to justify the function approach. @Dan: I really enjoyed your disclaimer :D -- Sorin Buturugeanu www.soin.ro http://www.facebook.com/buturugeanu http://www.twitter.com/soinrohttp://www.soin.ro/feed/blogblog: Despre Launch48 si ce poti face in 2 zile http://www.soin.ro/b75 On 17 December 2010 23:48, Daniel Brown danbr...@php.net wrote: On Fri, Dec 17, 2010 at 15:52, Sorin Buturugeanu m...@soin.ro wrote: Hello all! I have a question regarding arrays and the way I can use a value. Let's say I have this string: $s = 'banana,apple,mellon,grape,nut,orange' I want to explode it, and get the third value. For this I would normally do: $a = explode(',', $s); echo $s[2]; That's all fine, but is there a way to get the value directly, without having to write another line in my script. I mean something like this: echo explode(',', $s)[2]; or echo {explode(',', $s)}[2]; I couldn't find out this answer anywhere, that's why I posted here. Unfortunately, no --- at least, not yet. Chaining discussions come up now and again, so it's quite possible that future versions of PHP will have something similar. That said, for now you could do something like this: ?php /** * mixed return_item( string $car, mixed $pos ) * - $str The original string * - $charThe delimiting character(s) by which to explode * - $pos The position to return * - $shift Whether or not we should see 1 as the first array position */ function return_item($str,$char,$pos=null,$shift=false) { // Make sure $char exists in $str, return false if not. if (!strpos($str,$char)) return false; // Split $char by $str into the array $arr $arr = explode($char,$str); // If $pos undefined or null, return the whole array if (is_null($pos)) return $arr; // If $pos is an array, return the requested positions if (isset($pos) is_array($pos) !empty($pos)) { // Instantiate a second array container for return $ret = array(); // Iterate foreach ($pos as $i) { // This is just in case it was given screwy or a number as a non-integer if (!is_int($i) is_numeric($i)) $i = (int)round($i); // Make sure $i is now an integer and that position exists if (!is_int($i) || !isset($arr[$i]) || empty($arr[$i])) continue; // If all seems okay, append this to $ret $ret[] = $arr[$i]; } // Return the array return $ret; } /** * If $pos is a number (integer or round()'able number), * we'll go ahead and make sure the position is there. * If so, we'll return it. */ if (is_int($pos) || is_numeric($pos)) { // This is just in case it was given screwy or as a non-integer if (!is_int($pos)) $pos = (int)round($pos); // If we want to start the array count at 1, do that now if (isset($shift) ($shift === true || $shift === 1)) { // but only if the number isn't zero if ($pos !== 0) --$pos; } // Return the single position if it exists if (isset($arr[$pos]) !empty($arr[$pos])) return $arr[$pos]; } /** * If we've failed every case, something is either * wrong or we supplied bad data. Return false. * Either way, feel free to add some trigger_error() * stuff here if you want to have the function hold * your hand. */ return false; } /** * Some simple examples */ $foo = 'apple,banana,carrot,orange,carrot,lettuce,tomato,beer,carrot,idiot'; return_item($foo,',',7); // Returns 'beer' return_item($foo,'carrot',0); // Returns 'apple,banana,' return_item($foo,','); // Returns all items in an array return_item($foo,',',array(0,'2',6.6)); // Returns array: apple,carrot,beer return_item($foo,',',1,true); // Returns 'apple' ? Of course, as with almost all code I submit here, it's typed directly into this window and is untested, so you use it at your own risk, your mileage may vary, see a doctor if you have an erection lasting more than four hours, et cetera. Happy Friday, all. -- /Daniel P. Brown Network Infrastructure Manager Documentation, Webmaster Teams http://www.php.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] eval and HEREDOC
Hi Peter! In the first version of the template engine I had markers but now I need more complex conditions or operations inside the template files, like array of arrays and so on. I really like the freedom to be able to just open a PHP tag ? ? and use a foreach() or whatever PHP function as I please. Thanks again to all of you for your support! :) Sorin -- Sorin Buturugeanu http://www.soin.ro On Wed, Jul 21, 2010 at 9:47 AM, Peter Lind peter.e.l...@gmail.com wrote: On 21 July 2010 06:46, Sorin Buturugeanu m...@soin.ro wrote: @Vincent: no, the short tags are not off. @Jim: This seamns to work fine: $template = file_get_contents(pathTemplates.$this-dir.$this-tpl); ob_start(); $template = eval('?'.$template); $template = ob_get_clean(); Thanks! Best wishes! Sorin -- Sorin Buturugeanu http://www.soin.ro On Wed, Jul 21, 2010 at 1:45 AM, li...@cmsws.com wrote: On Wed, 21 Jul 2010 01:04:12 +0300, Sorin Buturugeanu m...@soin.ro wrote: Hello Vincent and thank you for your reply :). That's true, I forgot to explain how I got to using HEREDOC, so .. Using eval(file_get_contents($file)) just outputs the result on the spot and I need to get the whole output (without echoing it) and do some more things with it. require_once() doesn't fit here (from what I can tell), because it would still just include the file in echo the output. I think there must be a solution, but I'm missing something here .. Check out the ob_* functions You could do this ob_start(); include /your/file.php; $output = ob_get_clean(); echo $output; Jim Thanks again! -- Sorin Buturugeanu http://www.soin.ro On Wed, Jul 21, 2010 at 12:49 AM, Daevid Vincent dae...@daevid.com wrote: -Original Message- From: Sorin Buturugeanu [mailto:m...@soin.ro] Sent: Tuesday, July 20, 2010 2:11 PM To: php-general@lists.php.net Subject: [PHP] eval and HEREDOC Hello, I am having trouble with a part of my templating script. I'll try to explain: The template itself is HTML with PHP code inside it, like: div?=strtoupper($user['name']);?/div And I have the following code as part of the templating engine: $template = file_get_contents($file); $template = return TEMPLATE\n.$template.\nTEMPLATE;\n; $template = eval($template); The problem is that the eval() HEREDOC combination gives the following output: ?=strtoupper(Array['time']);? If in the HTML file (template) I use div?=strtoupper({$user['name']});?/div I get ?=strtoupper(username);? as an output. I have tried closing the php tag like this: $template = return TEMPLATE\n?.$template.\nTEMPLATE;\n; but the extra ? only gets outputed as HTML. This is my first post to this mailing list, so I great you all and thank you for any kind of solution to my problem. Why are you using HEREDOC to begin with? I personally find them to be ugly and more trouble than they're worth. You can write the same thing as this I think (untested): $template = eval(file_get_contents($file)); But you might also consider using include_once or require_once instead of this eval() business. Also note, that a string can span more than one line and have variables in it. It can even be used with code, so HEREDOC is again useless for most situations: $foo = Hello $name,\n \n Today is .date('Y-m-d').\n \n Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n Nulla eros purus, pharetra a blandit non, pellentesque et leo. In augue metus, mattis a sollicitudin in, placerat vitae elit. \n Quisque elit mauris, varius sit amet cursus sed, eleifend a mauris. ; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php 1) heredoc is great for not caring about escaping ' or . Whether or not you like the syntax it's not hard to use in any way. 2) stay away from eval. Just don't use it unless you have no other choice 3) the include+output buffering solution is much better. Another option would be to use markers in your templates and then just replace them with content. Whether or not that would suit you depends on what kinds of templates you're making and for what purpose Regards Peter -- hype WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind BeWelcome/Couchsurfing: Fake51 Twitter: http://twitter.com/kafe15 /hype -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] eval and HEREDOC
Hello, I am having trouble with a part of my templating script. I'll try to explain: The template itself is HTML with PHP code inside it, like: div?=strtoupper($user['name']);?/div And I have the following code as part of the templating engine: $template = file_get_contents($file); $template = return TEMPLATE\n.$template.\nTEMPLATE;\n; $template = eval($template); The problem is that the eval() HEREDOC combination gives the following output: ?=strtoupper(Array['time']);? If in the HTML file (template) I use div?=strtoupper({$user['name']});?/div I get ?=strtoupper(username);? as an output. I have tried closing the php tag like this: $template = return TEMPLATE\n?.$template.\nTEMPLATE;\n; but the extra ? only gets outputed as HTML. This is my first post to this mailing list, so I great you all and thank you for any kind of solution to my problem. Thank you! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] eval and HEREDOC
Hello Vincent and thank you for your reply :). That's true, I forgot to explain how I got to using HEREDOC, so .. Using eval(file_get_contents($file)) just outputs the result on the spot and I need to get the whole output (without echoing it) and do some more things with it. require_once() doesn't fit here (from what I can tell), because it would still just include the file in echo the output. I think there must be a solution, but I'm missing something here .. Thanks again! -- Sorin Buturugeanu http://www.soin.ro On Wed, Jul 21, 2010 at 12:49 AM, Daevid Vincent dae...@daevid.com wrote: -Original Message- From: Sorin Buturugeanu [mailto:m...@soin.ro] Sent: Tuesday, July 20, 2010 2:11 PM To: php-general@lists.php.net Subject: [PHP] eval and HEREDOC Hello, I am having trouble with a part of my templating script. I'll try to explain: The template itself is HTML with PHP code inside it, like: div?=strtoupper($user['name']);?/div And I have the following code as part of the templating engine: $template = file_get_contents($file); $template = return TEMPLATE\n.$template.\nTEMPLATE;\n; $template = eval($template); The problem is that the eval() HEREDOC combination gives the following output: ?=strtoupper(Array['time']);? If in the HTML file (template) I use div?=strtoupper({$user['name']});?/div I get ?=strtoupper(username);? as an output. I have tried closing the php tag like this: $template = return TEMPLATE\n?.$template.\nTEMPLATE;\n; but the extra ? only gets outputed as HTML. This is my first post to this mailing list, so I great you all and thank you for any kind of solution to my problem. Why are you using HEREDOC to begin with? I personally find them to be ugly and more trouble than they're worth. You can write the same thing as this I think (untested): $template = eval(file_get_contents($file)); But you might also consider using include_once or require_once instead of this eval() business. Also note, that a string can span more than one line and have variables in it. It can even be used with code, so HEREDOC is again useless for most situations: $foo = Hello $name,\n \n Today is .date('Y-m-d').\n \n Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n Nulla eros purus, pharetra a blandit non, pellentesque et leo. In augue metus, mattis a sollicitudin in, placerat vitae elit. \n Quisque elit mauris, varius sit amet cursus sed, eleifend a mauris. ; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] eval and HEREDOC
@Vincent: no, the short tags are not off. @Jim: This seamns to work fine: $template = file_get_contents(pathTemplates.$this-dir.$this-tpl); ob_start(); $template = eval('?'.$template); $template = ob_get_clean(); Thanks! Best wishes! Sorin -- Sorin Buturugeanu http://www.soin.ro On Wed, Jul 21, 2010 at 1:45 AM, li...@cmsws.com wrote: On Wed, 21 Jul 2010 01:04:12 +0300, Sorin Buturugeanu m...@soin.ro wrote: Hello Vincent and thank you for your reply :). That's true, I forgot to explain how I got to using HEREDOC, so .. Using eval(file_get_contents($file)) just outputs the result on the spot and I need to get the whole output (without echoing it) and do some more things with it. require_once() doesn't fit here (from what I can tell), because it would still just include the file in echo the output. I think there must be a solution, but I'm missing something here .. Check out the ob_* functions You could do this ob_start(); include /your/file.php; $output = ob_get_clean(); echo $output; Jim Thanks again! -- Sorin Buturugeanu http://www.soin.ro On Wed, Jul 21, 2010 at 12:49 AM, Daevid Vincent dae...@daevid.com wrote: -Original Message- From: Sorin Buturugeanu [mailto:m...@soin.ro] Sent: Tuesday, July 20, 2010 2:11 PM To: php-general@lists.php.net Subject: [PHP] eval and HEREDOC Hello, I am having trouble with a part of my templating script. I'll try to explain: The template itself is HTML with PHP code inside it, like: div?=strtoupper($user['name']);?/div And I have the following code as part of the templating engine: $template = file_get_contents($file); $template = return TEMPLATE\n.$template.\nTEMPLATE;\n; $template = eval($template); The problem is that the eval() HEREDOC combination gives the following output: ?=strtoupper(Array['time']);? If in the HTML file (template) I use div?=strtoupper({$user['name']});?/div I get ?=strtoupper(username);? as an output. I have tried closing the php tag like this: $template = return TEMPLATE\n?.$template.\nTEMPLATE;\n; but the extra ? only gets outputed as HTML. This is my first post to this mailing list, so I great you all and thank you for any kind of solution to my problem. Why are you using HEREDOC to begin with? I personally find them to be ugly and more trouble than they're worth. You can write the same thing as this I think (untested): $template = eval(file_get_contents($file)); But you might also consider using include_once or require_once instead of this eval() business. Also note, that a string can span more than one line and have variables in it. It can even be used with code, so HEREDOC is again useless for most situations: $foo = Hello $name,\n \n Today is .date('Y-m-d').\n \n Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n Nulla eros purus, pharetra a blandit non, pellentesque et leo. In augue metus, mattis a sollicitudin in, placerat vitae elit. \n Quisque elit mauris, varius sit amet cursus sed, eleifend a mauris. ; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php