Re: [PHP] String eval assistance
On 16 March 2011 00:25, Jack jacklistm...@gmail.com wrote: Here you're trying to access it as an array, which it's not, so the 'response' key doesn't exist. In addition, you're looking for UPPER-CASE, whereas that's not the case in your example variable. Finally, you're checking to make sure that the string IS INDEED found, but then printing that it was declined (!== false). Instead, you may want: ?php $results['response'] = '3434approd34'; if (stripos($results['response'],'APPROVED') !== false) { // It's been found } else { // Oh, crap. } ? maybe I should do this some other way because I'm getting false positives. I was using if(strpos($results['response'], 'APPROVED') !== false) { And its found if the value of $results = 3434APPROVED34 and it also is found if its $results = 3434APPOVED34, so this may not be the best way to accomplish this. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Can you create a small list of actual values and their results. What version of PHP are you using? -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String eval assistance
I'm not sure as to why strpos does what it does here, at least its not immediately obvious, but, a solution to this would be to use a regular expression search, it would be more exact, it has never failed me, and it will be faster; I recall reading that preg functions were faster at then str ones, though I can't recall where... -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. Richard Quadling rquadl...@gmail.com wrote: On 16 March 2011 00:25, Jack jacklistm...@gmail.com wrote: Here you're trying to access it as an array, which it's not, so the 'response' key doesn't exist. In addition, you're looking for UPPER-CASE, whereas that's not the case in your example variable. Finally, you're checking to make sure that the string IS INDEED found, but then printing that it was declined (!== false). Instead, you may want: ?php $results['response'] = '3434approd34'; if (stripos($results['response'],'APPROVED') !== false) { // It's been found } else { // Oh, crap. } ? maybe I should do this some other way because I'm getting false positives. I was using if(strpos($results['response'], 'APPROVED') !== false) { And its found if the value of $results = 3434APPROVED34 and it also is found if its $results = 3434APPOVED34, so this may not be the best way to accomplish this.-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Can you create a small list of actual values and their results. What version of PHP are you using? -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String eval assistance
On 16/03/2011, at 10:34 AM, Jack wrote: Hello All, I got some help on this yesterday, but somehow it's not consistant ? $results = 3434approd34; if(strpos($results['response'], 'APPROVED') !== false) { print declined; } else { print approved; } ? The thing is I cant get a consistant response, if it has approved anywhere in the results string, then it should be approved and if the results is APPROVD without the E it shold be delined. Am I doing something wrong. Thanks! Jack Yes, you're doing something wrong. strpos() returns false if it can't find the needle. You should be using if(strpos() === false) { declined; } --- Simon Welsh Admin of http://simon.geek.nz/ Who said Microsoft never created a bug-free program? The blue screen never, ever crashes! http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] String length output in php-generated response
-Original Message- From: Florin Jurcovici [mailto:florin.jurcov...@gmail.com] Sent: 06 February 2011 15:57 I'm trying to build myself a small JSON-RPC server using PHP. Using wireshark, here's the conversation: Request: [...snip...] Response: HTTP/1.1 200 OK Date: Sun, 06 Feb 2011 15:04:08 GMT Server: Apache/2.2.14 (Ubuntu) Accept-Ranges: bytes X-Powered-By: PHP/5.3.2-1ubuntu4.7 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: application/json; charset=UTF-8 6f {id:2,result:{service:test.service,method:method, id:2,params:[{code:client}]},error:null} 0 That's nothing to do with PHP -- it's http chunked encoding, as indicated by the Transfer-Encoding: chunked header, and is handled by Apache and your browser. It's totally expected and totally harmless. Read about it here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6 Cheers! Mike -- Mike Ford, Electronic Information Developer, Libraries and Learning Innovation, Leeds Metropolitan University, C507 City Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String length output in php-generated response
On 6 February 2011 15:57, Florin Jurcovici florin.jurcov...@gmail.com wrote: said it, Bush junior proved it Is this actually part of the output? -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Encodings - loose guidelines
Marc Guay wrote: 1.) Saving strings to a database One thing I always forget to remember is to send tge SET NAMES utf8 command to MySQL after making a connection. This will save you 1000 headaches if you're working with non-latin characters. I can't count the number of times I've thrown htmlentities, htmlspecialchars, utf8_encode/decode/, stripslashes, etc, etc around trying to figure out why those É's aren't being saved or read properly. I imagine this might fall into the category of best practice. Marc Thanks for the heads up! Donovan -- D Brooke -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Encodings - loose guidelines
1.) Saving strings to a database One thing I always forget to remember is to send tge SET NAMES utf8 command to MySQL after making a connection. This will save you 1000 headaches if you're working with non-latin characters. I can't count the number of times I've thrown htmlentities, htmlspecialchars, utf8_encode/decode/, stripslashes, etc, etc around trying to figure out why those É's aren't being saved or read properly. I imagine this might fall into the category of best practice. Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
David Harkness wrote: I've never used the old-style constructors, but perhaps the semantics of parent:: changed and you need to instead use $this- as in $this-Tag(option, $name); That's a total guess. I don't have 5.2 handy to try it out, but both work in 5.3 using a simple example. Can you post the constructor of one of the Tag subclasses that work? Maybe we can find a common denominator. The most similar is Column, but all of them do very similar things - it's just the one class that seems to take a string and mutate it into what looks like an array with a string at [0] and something closely resembling what the whole object instance *should* be at [1]. class Table extends Tag { function Table() { parent::Tag('table'); $this-addAttribute('cellspacing', 0); $this-addAttribute('cellpadding', 0); $this-addAttribute('border', 0); $this-columns = array(); $this-rows = array(); } class Row extends Tag { function Row($table='') { parent::Tag('tr'); $this-table = ''; } class Column extends Tag { function Column($data) { parent::Tag('td', $data); $this-tagContent = $data; } class FormObject extends Tag { function FormObject($name='') { parent::Tag(); $this-addAttribute(name, $name); $this-name = $name; } -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
Nathan Nobbe wrote: 2. try modifying Tag SelectBoxOption to have __construct() instead of Tag() SelectBoxOption(), then call parent::__construct() from inside of SelectBoxOption::__construct(); see if that clears up your problem under 5.2 (read: this will only be a partial solution as it only addresses one child of Tag). Mmm. I hoped this would help, but all it seems to have done was cascade errors across the rest of Tag's object children. :( to be expected, but did it fix the problem w/ SelectBoxOption? I'm not sure, but I don't think so; the original symptom was Object of class SelectBoxOption could not be converted to string - the original code didn't include a toString() method in SelectBoxOption, and since the code works on an older PHP, it must be using the parent object's toString(). (Which some children of Tag do explicitly, but SelectBoxOption doesn't for whatever reason.) In trying to add the toString() method, I found that the calls used by other tags to retrieve the HTML tag name, value, etc weren't working. So I looked up at the constructor to see if the pieces passed in were getting passed and stored correctly - and quite obviously they're not ($name mutates into what looks like an array with a string at [0] and something closely resembling what the whole object instance *should* be at [1], and $value just seems to disappear). Putting aside actually fixing the constructor correctly, after a bit of poking I found that $this-tagContent-. works to retrieve the data and actually output the option tag correctly. var_dump tells me that the real data is actually in there... it's just not instantiated correctly. $name apparently arrives at the constructor for SelectOptionBox like this: string(8) Abegweit object(SelectBoxOption)#65 (5) { [attributes]= array(1) { [0]= object(TagAttribute)#66 (3) { [name]= string(5) value [value]= string(1) 4 [hasValue]= bool(true) } } [tagContent]= string(8) Abegweit [tag]= string(6) option [showEndTag]= bool(false) [children]= array(0) { } } I'll try converting all of the constructors to your recommendation as above, but given that the problem is only happening with this one class, I'm not sure that will do much. hopefully that clears it up .. Well, I ran out of Call to undefined ParentClass::parentclass in path/to/file/for/subclass.php errors (at least on the page I'm testing with) but $name is still going in on the calling side as a string, and coming out as a funky array. and hopefully you're using version control :D Bah! Real man never make mistaaake! ... ooops. g (I've got the live site, on the old server, as reference, plus the regular backups of that machine.) -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] String passed to object constructor turning into an instance of that object?
-Original Message- From: Kris Deugau [mailto:kdeu...@vianet.ca] Sent: Thursday, December 16, 2010 11:57 AM To: php-general@lists.php.net Subject: [PHP] String passed to object constructor turning into an instance of that object? I'm in the process of migrating customer websites off an old legacy server that's pushing EOL, and starting to show hardware failures. One site is throwing errors on what, so far as I can tell, should be perfectly working code. The original code works fine on both CentOS 3 (PHP 4.3.2) and CentOS 4 (4.3.9); the new server is still a bit outdated (Debian etch plus some backports and updates from lenny; PHP 5.2.0). The site was designed by staff at a previous hosting company and uses a combination of the Fusebox app framework (which seems to work OK, after a few relatively minor fixes) and a custom OOP structure. I'm not really sure what the actual problem is, but I've reached the point where this: class SelectBoxOption extends Tag { function SelectBoxOption($name, $value, $selected=false) { parent::Tag(option, $name); $this-addAttribute(value, $value); if($selected) { $this-addAttribute(selected, '', false); } if ($name == ) { echo nbsp;nbsp;nbsp;missing name!br\n; } // else { print nbsp;nbsp;nbsp;name $namebr\n; } if ($value == ) { echo nbsp;nbsp;nbsp;missing value!br\n; } } will parse and execute, but: - the page will contain missing value! for each option in the select this is generating - it will *not* contain missing name! - the option tags in the final output don't have content or value (they should have both). If I uncomment that else, I get: adding option name1 with value1 name value1 Catchable fatal error: Object of class SelectBoxOption could not be converted to string in webroot/includes/classes/core/display/form/input/SelectBoxOption.php on line 12 What's the actual line #12 in the file SelectBoxOption.php? The SelectBoxOption code you presented has 11 lines unless it's a CNP error. Regards, Tommy I found the place this object is created, and added some debugging output before *and* after that call: echo adding option .$row-$nameField. with . $row-$valueField.br\n; $this-add(new SelectBoxOption($row-$nameField, $row-$valueField, $selected)); echo added option .$row-$nameField. with . $row-$valueField.br\n; which behaves correctly and spits out the name and value (retrieved from a database - thankfully I haven't had to track *that* down... yet). Can anyone explain why a string passed by value (apparently) would suddenly mutate into a SelectBoxOption object? I've confirmed that this is exactly what happens by adding this: if (is_a($name,'SelectBoxOption')) { print name isn't a SelectBoxOption, silly rabbit!br\n; } as the very next set of lines after function SelectBoxOption(. I wondered while typing this if $name and $value might have ended up as special variables somewhere, but renaming them with an opt_ prefix didn't change anything. -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into an instance of that object?
Tommy Pham wrote: class SelectBoxOption extends Tag { function SelectBoxOption($name, $value, $selected=false) { parent::Tag(option, $name); $this-addAttribute(value, $value); if($selected) { $this-addAttribute(selected, '', false); } if ($name == ) { echo nbsp;nbsp;nbsp;missing name!br\n; } // else { print nbsp;nbsp;nbsp;name $namebr\n; } if ($value == ) { echo nbsp;nbsp;nbsp;missing value!br\n; } } will parse and execute, but: - the page will contain missing value! for each option in the select this is generating - it will *not* contain missing name! - the option tags in the final output don't have content or value (they should have both). If I uncomment that else, I get: adding option name1 with value1 name value1 Catchable fatal error: Object of class SelectBoxOption could not be converted to string in webroot/includes/classes/core/display/form/input/SelectBoxOption.php on line 12 What's the actual line #12 in the file SelectBoxOption.php? The SelectBoxOption code you presented has 11 lines unless it's a CNP error. Whups, thought I noted that. I trimmed a couple of blank lines; line 12 in the file is that print in the else. I found trying to print $name triggers the same error anywhere in that function, too; as I noted further down it seems the string that's passed in is getting mutated into an object. (Whose missing toString function is what led me here - but it works fine in PHP 4.3...) -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
Nathan Nobbe wrote: Why not test for the type of $name at each point of interest in the SelectBoxOption constructor? If you're passing a string value to the constructor it almost has to be getting changed by the Tag constructor, right ? class SelectBoxOption extends Tag { function SelectBoxOption($name, $value, $selected=false) { var_dump(is_string($name)); parent::Tag(option, $name); var_dump(is_string($name)); Ah, that gives... well, it slightly alters the confusion. Using var_dump(is_string($name)) gives... two results? bool(true) bool(false) And dumping $name itself gives: string(8) Abegweit object(SelectBoxOption)#65 (5) { [attributes]= array(1) { [0]= object(TagAttribute)#66 (3) { [name]= string(5) value [value]= string(1) 4 [hasValue]= bool(true) } } [tagContent]= string(8) Abegweit [tag]= string(6) option [showEndTag]= bool(false) [children]= array(0) { } } O_o Just to confirm, I checked a test instance of the site on CentOS 4, with PHP 4.3, and I get one bool(true) for each option - not two as is happening with PHP 5.2. -kgd (I haven't worked with PHP for quite a while, and I never really spent a lot of time getting deep into complex data structures and object hierarchies like this when I was using it. But this behaviour does NOT match what I know of passing values and object references around in any other language.) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into aninstance of that object?
On Thu, Dec 16, 2010 at 3:21 PM, Kris Deugau kdeu...@vianet.ca wrote: Nathan Nobbe wrote: Why not test for the type of $name at each point of interest in the SelectBoxOption constructor? If you're passing a string value to the constructor it almost has to be getting changed by the Tag constructor, right ? class SelectBoxOption extends Tag { function SelectBoxOption($name, $value, $selected=false) { var_dump(is_string($name)); parent::Tag(option, $name); var_dump(is_string($name)); Ah, that gives... well, it slightly alters the confusion. Using var_dump(is_string($name)) gives... two results? bool(true) bool(false) And dumping $name itself gives: string(8) Abegweit object(SelectBoxOption)#65 (5) { [attributes]= array(1) { [0]= object(TagAttribute)#66 (3) { [name]= string(5) value [value]= string(1) 4 [hasValue]= bool(true) } } [tagContent]= string(8) Abegweit [tag]= string(6) option [showEndTag]= bool(false) [children]= array(0) { } } O_o Just to confirm, I checked a test instance of the site on CentOS 4, with PHP 4.3, and I get one bool(true) for each option - not two as is happening with PHP 5.2. probly something screwy going on w/ the old style of naming constructors. 2 things, 1. can you post the Tag constructor as it reads now? 2. try modifying Tag SelectBoxOption to have __construct() instead of Tag() SelectBoxOption(), then call parent::__construct() from inside of SelectBoxOption::__construct(); see if that clears up your problem under 5.2 (read: this will only be a partial solution as it only addresses one child of Tag). -kgd (I haven't worked with PHP for quite a while, and I never really spent a lot of time getting deep into complex data structures and object hierarchies like this when I was using it. But this behaviour does NOT match what I know of passing values and object references around in any other language.) Probly because the term 'reference' in php means something rather different than it does in say java for example.
Re: [PHP] String passed to object constructor turning into aninstance of that object?
It's acting as if Tag's constructor a) declares $name as a reference using $name, and b) is assigning itself ($this) to $name for some (probably bad) reason. That's the only way I can see that $name inside SelectBoxOption's constructor could change from a string to an object. A peek at Tag's constructor could really clear things up. David
Re: [PHP] String passed to object constructor turning into an instance of that object?
Nathan Nobbe wrote: probly something screwy going on w/ the old style of naming constructors. 2 things, 1. can you post the Tag constructor as it reads now? function Tag($tag='', $tagContent='') { $this-tagContent = $tagContent; $this-tag = $tag; $this-showEndTag = false; $this-attributes = array(); $this-children = array(); } 2. try modifying Tag SelectBoxOption to have __construct() instead of Tag() SelectBoxOption(), then call parent::__construct() from inside of SelectBoxOption::__construct(); see if that clears up your problem under 5.2 (read: this will only be a partial solution as it only addresses one child of Tag). Mmm. I hoped this would help, but all it seems to have done was cascade errors across the rest of Tag's object children. :( Copying the old constructor back in resolved that, but I'm not sure whether that reintroduces the root problem. Other objects derived from Tag seem to work just fine; I came into this chunk of the code trying to find out why a SelectBoxOption didn't seem to have a toString function - and then why trying to access what should be the value and name the same way as with other objects derived at some level from Tag blew up instead of working happily. I'll try converting all of the constructors to your recommendation as above, but given that the problem is only happening with this one class, I'm not sure that will do much. (A don't-break-crusty-old-code option for php.ini would be handy...) The class hierarchy I've dug up so far looks like this (and appears to have been entirely defined by the original developer): Object Fieldset RadioButtonGroup Tag Column FormObject FormInput CheckBox DateSelector Editor FileField FormButton HiddenField PasswordField RadioButton SelectBox PopulatedSelectBox RecursiveSelectBox TextArea TextField Form Row Table SelectBoxOption -kgd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String passed to object constructor turning into an instance of that object?
On Thu, Dec 16, 2010 at 4:04 PM, Kris Deugau kdeu...@vianet.ca wrote: Nathan Nobbe wrote: probly something screwy going on w/ the old style of naming constructors. 2 things, 1. can you post the Tag constructor as it reads now? function Tag($tag='', $tagContent='') { $this-tagContent = $tagContent; $this-tag = $tag; $this-showEndTag = false; $this-attributes = array(); $this-children = array(); } seems innocuous .. 2. try modifying Tag SelectBoxOption to have __construct() instead of Tag() SelectBoxOption(), then call parent::__construct() from inside of SelectBoxOption::__construct(); see if that clears up your problem under 5.2 (read: this will only be a partial solution as it only addresses one child of Tag). Mmm. I hoped this would help, but all it seems to have done was cascade errors across the rest of Tag's object children. :( to be expected, but did it fix the problem w/ SelectBoxOption? Copying the old constructor back in resolved that, but I'm not sure whether that reintroduces the root problem. Other objects derived from Tag seem to work just fine; I came into this chunk of the code trying to find out why a SelectBoxOption didn't seem to have a toString function - and then why trying to access what should be the value and name the same way as with other objects derived at some level from Tag blew up instead of working happily. I'll try converting all of the constructors to your recommendation as above, but given that the problem is only happening with this one class, I'm not sure that will do much. hopefully that clears it up .. and hopefully you're using version control :D -nathan
Re: [PHP] String passed to object constructor turning into an instance of that object?
I've never used the old-style constructors, but perhaps the semantics of parent:: changed and you need to instead use $this- as in $this-Tag(option, $name); That's a total guess. I don't have 5.2 handy to try it out, but both work in 5.3 using a simple example. Can you post the constructor of one of the Tag subclasses that work? Maybe we can find a common denominator. David
Re: [PHP] String passed to object constructor turning into aninstance of that object?
On Thu, Dec 16, 2010 at 3:21 PM, Kris Deugau kdeu...@vianet.ca wrote: Nathan Nobbe wrote: Why not test for the type of $name at each point of interest in the SelectBoxOption constructor? If you're passing a string value to the constructor it almost has to be getting changed by the Tag constructor, right ? class SelectBoxOption extends Tag { function SelectBoxOption($name, $value, $selected=false) { var_dump(is_string($name)); parent::Tag(option, $name); var_dump(is_string($name)); Ah, that gives... well, it slightly alters the confusion. Using var_dump(is_string($name)) gives... two results? bool(true) bool(false) so you put one check before the call to parent::Tag() one directly after right? That means *somehow* $name is getting set to an instance of SelectBoxOption in the parent constructor which makes little to no sense.. especially after looking at implementation from your later post. Main things are $name is local in the child constructor and there is no pass by reference on the $name parameter in the parent constructor definition. if this code runs w/o error on your 5.2 box, then there's something spurious going on in that old library; ?php class Tag { function Tag($sTag='', $sValue='') { $this-_sTag = $sTag; $this-_sValue = $sValue; } } class Child extends Tag { function Child($name) { var_dump($name); parent::Tag('option', $name); var_dump($name); } } $oChild = new Child('content'); ? expected output: string(7) content string(7) content I'd still recommend moving to the php5 notation throughout the library, especially if doing that fixes the problem w/ SelectBoxOption. This shouldn't break any client code, since clients should all be calling new Class() and not be explicitly invoking the php4 style constructors. The php4 style constructors should only be getting called explicitly from within the library itself. -nathan
Re: [PHP] String Parse Help for novice
At 9:29 PM -0400 6/13/10, Robert Cummings wrote: ?php function my_parse_url( $url ) { $parsed = parse_url( $url ); $parsed['file'] = basename( $parsed['path'] ); $parsed['pathbits'] = explode( '/', ltrim( dirname( $parsed['path'] ), '/' ) ); return $parsed; } $url = my_parse_url( 'http://foo.fee.com/blah/bleh/bluh/meh.php' ); print_r( $url ); ? Cheers, Rob. Rob: Very neat. It also handles url's like this: http://mydomain.com/mydirectory/mysubdirectory/anothersubdirectory/mypage.php See Demo here: http://www.webbytedd.com/b4/parse-url/index.php Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
tedd wrote: At 9:29 PM -0400 6/13/10, Robert Cummings wrote: ?php function my_parse_url( $url ) { $parsed = parse_url( $url ); $parsed['file'] = basename( $parsed['path'] ); $parsed['pathbits'] = explode( '/', ltrim( dirname( $parsed['path'] ), '/' ) ); return $parsed; } $url = my_parse_url( 'http://foo.fee.com/blah/bleh/bluh/meh.php' ); print_r( $url ); ? Cheers, Rob. Rob: Very neat. It also handles url's like this: http://mydomain.com/mydirectory/mysubdirectory/anothersubdirectory/mypage.php See Demo here: http://www.webbytedd.com/b4/parse-url/index.php It's useful to leverage the work of others. So using parse_url() gets you all the parsing stuff for a url without having to worry about the spec (such as embedded user, password, port, parameters, and fragment. Then we just augment to provide the extra functionality :) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:13 PM, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Hi Rick, Just a thought, but cant you do something to separate them according to the / (forward slash)? maybe preg_replace or something. Sorry not much more help. Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Hi Ash, What about the // in the beginning? Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Sun, 2010-06-13 at 17:27 -0500, Karl DeSaulniers wrote: On Jun 13, 2010, at 5:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Hi Ash, What about the // in the beginning? Karl DeSaulniers Design Drumm http://designdrumm.com As your example string didn't have a double slash I didn't write code for that, but it's easy enough to remove 0-length strings from the $directories array. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:31 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 17:27 -0500, Karl DeSaulniers wrote: On Jun 13, 2010, at 5:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Hi Ash, What about the // in the beginning? Karl DeSaulniers Design Drumm http://designdrumm.com As your example string didn't have a double slash I didn't write code for that, but it's easy enough to remove 0-length strings from the $directories array. Thanks, Ash http://www.ashleysheridan.co.uk :) Rick's example, but how in your example do we look for a double forward slash? THX Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
OK, I get the following error: Warning: basename() expects parameter 1 to be string, array given in When I use the following: $thepath = parse_url($url); $filename = basename($thepath); Is my variable thepath not automatically string? --Rick On Jun 13, 2010, at 6:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:35 PM, Rick Dwyer wrote: OK, I get the following error: Warning: basename() expects parameter 1 to be string, array given in When I use the following: $thepath = parse_url($url); $filename = basename($thepath); Is my variable thepath not automatically string? --Rick try echo($url); and see Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Sun, 2010-06-13 at 17:35 -0500, Karl DeSaulniers wrote: On Jun 13, 2010, at 5:31 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 17:27 -0500, Karl DeSaulniers wrote: On Jun 13, 2010, at 5:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Hi Ash, What about the // in the beginning? Karl DeSaulniers Design Drumm http://designdrumm.com As your example string didn't have a double slash I didn't write code for that, but it's easy enough to remove 0-length strings from the $directories array. Thanks, Ash http://www.ashleysheridan.co.uk :) Rick's example, but how in your example do we look for a double forward slash? THX Karl DeSaulniers Design Drumm http://designdrumm.com You don't look for one, that's the point. The explode() breaks the string into an array at every occurrence of a '/' character. This will leave zero length strings in the array if there is a double // (which wasn't in any given example in this thread that I saw) When you use the array, just don't do anything with empty elements! Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:35 PM, Rick Dwyer wrote: OK, I get the following error: Warning: basename() expects parameter 1 to be string, array given in When I use the following: $thepath = parse_url($url); $filename = basename($thepath); Is my variable thepath not automatically string? --Rick Oops I meant echo($the_path); or echo both and see. Karl DeSaulniers Design Drumm http://designdrumm.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Sun, 2010-06-13 at 18:35 -0400, Rick Dwyer wrote: OK, I get the following error: Warning: basename() expects parameter 1 to be string, array given in When I use the following: $thepath = parse_url($url); $filename = basename($thepath); Is my variable thepath not automatically string? --Rick On Jun 13, 2010, at 6:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Because you've given it an array. Your original question never mentioned you were using parse_url() on the original array string. parse_url() breaks the string into its component parts, much like my explode example. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] String Parse Help for novice
On Jun 13, 2010, at 5:40 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 17:35 -0500, Karl DeSaulniers wrote: On Jun 13, 2010, at 5:31 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 17:27 -0500, Karl DeSaulniers wrote: On Jun 13, 2010, at 5:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Hi Ash, What about the // in the beginning? Karl DeSaulniers Design Drumm http://designdrumm.com As your example string didn't have a double slash I didn't write code for that, but it's easy enough to remove 0-length strings from the $directories array. Thanks, Ash http://www.ashleysheridan.co.uk :) Rick's example, but how in your example do we look for a double forward slash? THX Karl DeSaulniers Design Drumm http://designdrumm.com You don't look for one, that's the point. The explode() breaks the string into an array at every occurrence of a '/' character. This will leave zero length strings in the array if there is a double // (which wasn't in any given example in this thread that I saw) When you use the array, just don't do anything with empty elements! Thanks, Ash http://www.ashleysheridan.co.uk Ahh.. that makes sense. Thanks Ash. Karl DeSaulniers Design Drumm http://designdrumm.com
Re: [PHP] String Parse Help for novice
OK, sorry for any confusion. Here is all my code: $url = http . ((!empty($_SERVER['HTTPS'])) ? s : ) . ://. $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; $thepath = parse_url($url); So, given that the URL can vary as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. --Rick On Jun 13, 2010, at 6:42 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:35 -0400, Rick Dwyer wrote: OK, I get the following error: Warning: basename() expects parameter 1 to be string, array given in When I use the following: $thepath = parse_url($url); $filename = basename($thepath); Is my variable thepath not automatically string? --Rick On Jun 13, 2010, at 6:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Because you've given it an array. Your original question never mentioned you were using parse_url() on the original array string. parse_url() breaks the string into its component parts, much like my explode example. Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Sun, 2010-06-13 at 18:52 -0400, Rick Dwyer wrote: OK, sorry for any confusion. Here is all my code: $url = http . ((!empty($_SERVER['HTTPS'])) ? s : ) . ://. $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; $thepath = parse_url($url); So, given that the URL can vary as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. --Rick On Jun 13, 2010, at 6:42 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:35 -0400, Rick Dwyer wrote: OK, I get the following error: Warning: basename() expects parameter 1 to be string, array given in When I use the following: $thepath = parse_url($url); $filename = basename($thepath); Is my variable thepath not automatically string? --Rick On Jun 13, 2010, at 6:23 PM, Ashley Sheridan wrote: On Sun, 2010-06-13 at 18:13 -0400, Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. --Rick $filename = basename($path); $parts = explode('/', $path); $directories = array_pop($parts); Now you have your directories in the $directories array and the filename in $filename. Thanks, Ash http://www.ashleysheridan.co.uk Because you've given it an array. Your original question never mentioned you were using parse_url() on the original array string. parse_url() breaks the string into its component parts, much like my explode example. Thanks, Ash http://www.ashleysheridan.co.uk Take out the parse_url line and use the code I gave you, or keep the parse_url line and drop my explode line. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] String Parse Help for novice
Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. ?php function my_parse_url( $url ) { $parsed = parse_url( $url ); $parsed['file'] = basename( $parsed['path'] ); $parsed['pathbits'] = explode( '/', ltrim( dirname( $parsed['path'] ), '/' ) ); return $parsed; } $url = my_parse_url( 'http://foo.fee.com/blah/bleh/bluh/meh.php' ); print_r( $url ); ? Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Parse Help for novice
On Sun, Jun 13, 2010 at 9:29 PM, Robert Cummings rob...@interjinn.comwrote: Rick Dwyer wrote: Hello List. I need to parse the PATH portion of URL. I have assigned the path portion to a variable using the following: $thepath = parse_url($url); Now I need to break each portion of the path down into its own variable. The problem is, the path can vary considerably as follows: /mydirectory/mysubdirectory/anothersubdirectory/mypage.php vs. /mydirectory/mypage.php How do I get the either of the above url paths broken out so the variables equal the following $dir1 = mydirectory $dir2 = mysubdirectory $dir3 = anothersubdirectory $page = mypage.php ...etc... if there were 5 more subdirectories... they would be dynamically assigned to a variable. Thanks for any help. ?php function my_parse_url( $url ) { $parsed = parse_url( $url ); $parsed['file'] = basename( $parsed['path'] ); $parsed['pathbits'] = explode( '/', ltrim( dirname( $parsed['path'] ), '/' ) ); return $parsed; } $url = my_parse_url( 'http://foo.fee.com/blah/bleh/bluh/meh.php' ); print_r( $url ); ? Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Clean and lean, Robert ;) Nice! Adam -- Nephtali: PHP web framework that functions beautifully http://nephtaliproject.com
Re: [PHP] string concatenation with fgets
aurfal...@gmail.com wrote: So here is my final test code, notice the check for ' ' in the if. Since I'm on Linux, this has to do with whats between the last LF and EOF which is nothing but this nothing will get printed out. $file = fopen(somefile.txt, r); while (! feof($file)) { $tmp = trim(fgets($file)); if ($tmp != '') { $names = $tmp; } print $names.sometext\n; } fclose($file); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string concatenation with fgets
Hi Shawn, Your code looks cleaner then mine so i tried it and got the last entry in the txt file printed twice. On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote: aurfal...@gmail.com wrote: So here is my final test code, notice the check for ' ' in the if. Since I'm on Linux, this has to do with whats between the last LF and EOF which is nothing but this nothing will get printed out. $file = fopen(somefile.txt, r); while (! feof($file)) { $tmp = trim(fgets($file)); if ($tmp != '') { $names = $tmp; } print $names.sometext\n; } fclose($file); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string concatenation with fgets
On Mon, 2009-11-30 at 09:04 -0800, aurfal...@gmail.com wrote: Hi Shawn, Your code looks cleaner then mine so i tried it and got the last entry in the txt file printed twice. On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote: aurfal...@gmail.com wrote: So here is my final test code, notice the check for ' ' in the if. Since I'm on Linux, this has to do with whats between the last LF and EOF which is nothing but this nothing will get printed out. $file = fopen(somefile.txt, r); while (! feof($file)) { $tmp = trim(fgets($file)); if ($tmp != '') { $names = $tmp; } print $names.sometext\n; } fclose($file); -- Thanks! -Shawn http://www.spidean.com Remove the if statement and just print out $tmp. The while loop is going over one extra time than you need, and on that final iteration, $tmp is an empty string. The if statement only changes $name if $tmp is empty, so it leaves it as it was, hence you getting the last line printed twice. Printing out an empty string in this example won't do anything, and the if statement is also pretty useless as it just copies the value to another variable on a condition that will only result in the side-effect you've noticed. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] string concatenation with fgets
Hi Ash, Actually I need the if because the code will print out an empty line and add sometext to it. So without the if check for an empty line, at the end of the loop I'll get sometext. For example, if the file I am processing called somename.txt has a b c in it. I'll have; asometext bsometext csometext but w/o the if check, I'll also have sometext as well. On Nov 30, 2009, at 9:24 AM, Ashley Sheridan wrote: On Mon, 2009-11-30 at 09:04 -0800, aurfal...@gmail.com wrote: Hi Shawn, Your code looks cleaner then mine so i tried it and got the last entry in the txt file printed twice. On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote: aurfal...@gmail.com wrote: So here is my final test code, notice the check for ' ' in the if. Since I'm on Linux, this has to do with whats between the last LF and EOF which is nothing but this nothing will get printed out. $file = fopen(somefile.txt, r); while (! feof($file)) { $tmp = trim(fgets($file)); if ($tmp != '') { $names = $tmp; } print $names.sometext\n; } fclose($file); -- Thanks! -Shawn http://www.spidean.com Remove the if statement and just print out $tmp. The while loop is going over one extra time than you need, and on that final iteration, $tmp is an empty string. The if statement only changes $name if $tmp is empty, so it leaves it as it was, hence you getting the last line printed twice. Printing out an empty string in this example won't do anything, and the if statement is also pretty useless as it just copies the value to another variable on a condition that will only result in the side-effect you've noticed. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] string concatenation with fgets
On Mon, 2009-11-30 at 09:40 -0800, aurfal...@gmail.com wrote: Hi Ash, Actually I need the if because the code will print out an empty line and add sometext to it. So without the if check for an empty line, at the end of the loop I'll get sometext. For example, if the file I am processing called somename.txt has a b c in it. I'll have; asometext bsometext csometext but w/o the if check, I'll also have sometext as well. On Nov 30, 2009, at 9:24 AM, Ashley Sheridan wrote: On Mon, 2009-11-30 at 09:04 -0800, aurfal...@gmail.com wrote: Hi Shawn, Your code looks cleaner then mine so i tried it and got the last entry in the txt file printed twice. On Nov 30, 2009, at 7:07 AM, Shawn McKenzie wrote: aurfal...@gmail.com wrote: So here is my final test code, notice the check for ' ' in the if. Since I'm on Linux, this has to do with whats between the last LF and EOF which is nothing but this nothing will get printed out. $file = fopen(somefile.txt, r); while (! feof($file)) { $tmp = trim(fgets($file)); if ($tmp != '') { $names = $tmp; } print $names.sometext\n; } fclose($file); -- Thanks! -Shawn http://www.spidean.com Remove the if statement and just print out $tmp. The while loop is going over one extra time than you need, and on that final iteration, $tmp is an empty string. The if statement only changes $name if $tmp is empty, so it leaves it as it was, hence you getting the last line printed twice. Printing out an empty string in this example won't do anything, and the if statement is also pretty useless as it just copies the value to another variable on a condition that will only result in the side-effect you've noticed. Thanks, Ash http://www.ashleysheridan.co.uk Then put the print statement inside the if, not the assignation, otherwise you will always get that last line! Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] string concatenation with fgets
So here is my final test code, notice the check for ' ' in the if. Since I'm on Linux, this has to do with whats between the last LF and EOF which is nothing but this nothing will get printed out. $file = fopen(somefile.txt, r); while (! feof($file)) { $names = trim(fgets($file)); if ($names == '') { break; } print $names.sometext\n; } fclose($file); - aurf On Nov 24, 2009, at 5:52 PM, ryan wrote: Is this what you want $file = fopen(test.txt, r); while (!feof($file)) { $line = trim(fgets($file)); print $line.sometext\n; } fclose($file); outputs asometext bsometext csometext Ref to http://us3.php.net/manual/en/function.fgets.php. Reading ends when /length/ - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line. aurfal...@gmail.com wrote: Hi all, I'm trying to append some text to what I read from a file. My code; $file = fopen(foo.txt, r); while (!feof($file)) { $line = fgets($file); print $line.sometext; } fclose($file); foo,txt; a b c d e f g And when I run the script, it looks like; a sometextb sometextc sometextd ... Any ideas? Thanks in advance, - aurf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string concatenation with fgets
Is this what you want $file = fopen(test.txt, r); while (!feof($file)) { $line = trim(fgets($file)); print $line.sometext\n; } fclose($file); outputs asometext bsometext csometext Ref to http://us3.php.net/manual/en/function.fgets.php. Reading ends when /length/ - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line. aurfal...@gmail.com wrote: Hi all, I'm trying to append some text to what I read from a file. My code; $file = fopen(foo.txt, r); while (!feof($file)) { $line = fgets($file); print $line.sometext; } fclose($file); foo,txt; a b c d e f g And when I run the script, it looks like; a sometextb sometextc sometextd ... Any ideas? Thanks in advance, - aurf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string concatenation with fgets
--- On Wed, 11/25/09, aurfal...@gmail.com aurfal...@gmail.com wrote: From: aurfal...@gmail.com aurfal...@gmail.com Subject: [PHP] string concatenation with fgets To: php-general@lists.php.net Date: Wednesday, November 25, 2009, 7:00 AM Hi all, I'm trying to append some text to what I read from a file. My code; $file = fopen(foo.txt, r); while (!feof($file)) { $line = fgets($file); print $line.sometext; } fclose($file); foo,txt; a b c d e f g And when I run the script, it looks like; a sometextb sometextc sometextd ... Any ideas? So, what output you actually wants from your program? Is it like this asometextbsometextcsometext.. or, like this asometext bsometext csometext --- নির্মাল্য লাহিড়ী [Nirmalya Lahiri] +৯১-৯৪৩৩১১৩৫৩৬ [+91-9433113536] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string concatenation with fgets
On Nov 24, 2009, at 5:55 PM, Nirmalya Lahiri wrote: --- On Wed, 11/25/09, aurfal...@gmail.com aurfal...@gmail.com wrote: From: aurfal...@gmail.com aurfal...@gmail.com Subject: [PHP] string concatenation with fgets To: php-general@lists.php.net Date: Wednesday, November 25, 2009, 7:00 AM Hi all, I'm trying to append some text to what I read from a file. My code; $file = fopen(foo.txt, r); while (!feof($file)) { $line = fgets($file); print $line.sometext; } fclose($file); foo,txt; a b c d e f g And when I run the script, it looks like; a sometextb sometextc sometextd ... Any ideas? So, what output you actually wants from your program? Is it like this asometextbsometextcsometext.. or, like this asometext bsometext csometext Hi, Sorry, I was incomplete :) I would like; asometext bsometext csometext Basically, I would like to add whatever text to the end of what I find in the file. So if the file contains a b c I would like; asometext bsometext csometext... - aurf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string concatenation with fgets
On Nov 24, 2009, at 5:52 PM, ryan wrote: Is this what you want $file = fopen(test.txt, r); while (!feof($file)) { $line = trim(fgets($file)); print $line.sometext\n; } fclose($file); outputs asometext bsometext csometext Ref to http://us3.php.net/manual/en/function.fgets.php. Reading ends when /length/ - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line. aurfal...@gmail.com wrote: Hi all, I'm trying to append some text to what I read from a file. My code; $file = fopen(foo.txt, r); while (!feof($file)) { $line = fgets($file); print $line.sometext; } fclose($file); foo,txt; a b c d e f g And when I run the script, it looks like; a sometextb sometextc sometextd ... Any ideas? Thanks in advance, - aurf OMG, very very cool, thanks Ryan. - aurf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: PHP String convention
Nick Cooper wrote: Hi, I was just wondering what the difference/advantage of these two methods of writing a string are: 1) $string = foo{$bar}; 2) $string = 'foo'.$bar; 1) breaks PHPUnit when used in classes (need to bug report that) 2) [concatenation] is faster (but you wouldn't notice) comes down to personal preference and what looks best in your (teams) IDE I guess; legibility (and possibly portability) is probably the primary concern. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: PHP String convention
2009/11/4 Nathan Rixham nrix...@gmail.com: Nick Cooper wrote: Hi, I was just wondering what the difference/advantage of these two methods of writing a string are: 1) $string = foo{$bar}; 2) $string = 'foo'.$bar; 1) breaks PHPUnit when used in classes (need to bug report that) 2) [concatenation] is faster (but you wouldn't notice) comes down to personal preference and what looks best in your (teams) IDE I guess; legibility (and possibly portability) is probably the primary concern. I would tend to agree here; the concat is faster but you may well only notice in very tight loops. The curly brace syntax can increase code readability, depending on the complexity of the expression. I use them both depending on the situation. Remember the rules of optimization: 1) Don't. 2) (Advanced users only): Optimize later. Write code so that it's readable, and then once it's working, identify the bottlenecks and optimize where needed. If you understand code analysis and big-O etc then you will start to automatically write mostly-optimized code anyway and in general, I doubt that you'll often identify the use of double quotes as a bottleneck--it almost always turns out that other operations and code structures are far more expensive and impact code speed much more. That said, you don't really lose anything by using concatenation from the start, except perhaps some legibility, so as Nathan said it often really just comes down to personal preference and perhaps the house coding conventions. Regards, Torben 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
Re: [PHP] String scrambling
!niBgo /* $str = Bingo!; str_shuffle($str); */ :)
Re: [PHP] String scrambling
On Fri, 2009-09-11 at 11:03 +0100, Tom Chubb wrote: !niBgo /* $str = Bingo!; str_shuffle($str); */ :) No, that won't work at all, it's in comments ;) Thanks, Ash http://www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String scrambling
On Thu, Sep 10, 2009 at 8:57 PM, Ron Piggott ron@actsministries.org wrote: Is there a function in PHP which scrambles strings? Example: $string = Hello; Output might be: ehlol Ron http://www.php.net/manual/en/function.str-shuffle.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String to Date Conversion Problem
Hi Alice, Based on the string format that you mentioned (DD MMM YY - DAY) you should be able to transform to any other date using the following: $parts = explode(' ', '23 JUL 09 - THURSDAY'); echo date('m/d/Y', strtotime({$parts[1]} {$parts[0]} {$parts[2]})); Cheers Stuart On 31 Jul 2009, at 15:19, Alice Wei wrote: Hi, Guys: I am trying to turn a prepared line into a date format, and the string looks something like this: 23 JUL 09 - THURSDAY, and I am trying to change the string to a mm/dd/ format that looks like 07/23/2009. I tried to use strtotime() but it gave me nothing. Here is the code: list($date,$month,$year,$dash,$day) = split( ,$line,5); echo date2 . strtotime($date . \s . $month . \s . $year). /date2; Could anyone on the list please give me a hint on what I might have done wrong here? Thanks for your help. Alice _ All-in-one security and maintenance for your PC. Get a free 90-day trial! http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail smime.p7s Description: S/MIME cryptographic signature
RE: [PHP] String to Date Conversion Problem
Looks like what I did by using mm/dd/ was extra, which was probably why it didn't work. Thanks, looks like this is up and running now. Alice CC: php-general@lists.php.net From: stu...@stuconnolly.com To: aj...@alumni.iu.edu Subject: Re: [PHP] String to Date Conversion Problem Date: Fri, 31 Jul 2009 15:45:44 +0100 Hi Alice, Based on the string format that you mentioned (DD MMM YY - DAY) you should be able to transform to any other date using the following: $parts = explode(' ', '23 JUL 09 - THURSDAY'); echo date('m/d/Y', strtotime({$parts[1]} {$parts[0]} {$parts[2]})); Cheers Stuart On 31 Jul 2009, at 15:19, Alice Wei wrote: Hi, Guys: I am trying to turn a prepared line into a date format, and the string looks something like this: 23 JUL 09 - THURSDAY, and I am trying to change the string to a mm/dd/ format that looks like 07/23/2009. I tried to use strtotime() but it gave me nothing. Here is the code: list($date,$month,$year,$dash,$day) = split( ,$line,5); echo date2 . strtotime($date . \s . $month . \s . $year). /date2; Could anyone on the list please give me a hint on what I might have done wrong here? Thanks for your help. Alice _ All-in-one security and maintenance for your PC. Get a free 90-day trial! http://www.windowsonecare.com/purchase/trial.aspx?sc_cid=wl_wlmail _ Express yourself with gadgets on Windows Live Spaces http://discoverspaces.live.com?source=hmtag1loc=us
Re: [PHP] String variable
On Sun, Jan 11, 2009 at 8:59 AM, MikeP mpel...@princeton.edu wrote: Hello, I am trying yo get THIS: where ref_id = '1234' from this. $where=where ref_id=.'$Reference[$x][ref_id]'; but i certainly have a quote problem. Any help? Thanks Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Looks like you're missing the single tics around ref_id. In your example you'd want one of these: $where=where ref_id=.'{$Reference[$x]['ref_id']}; $where=where ref_id=.$Reference[$x]['ref_id']; $where=sprintf(where ref_id=%d, (int)$Reference[$x]['ref_id']); -- use this one or else! (sql injection) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String variable
On Sun, 2009-01-11 at 08:59 -0500, MikeP wrote: Hello, I am trying yo get THIS: where ref_id = '1234' from this. $where=where ref_id=.'$Reference[$x][ref_id]'; but i certainly have a quote problem. Any help? Thanks Mike It should look like this: $where=where ref_id='{$Reference[$x][ref_id]}'; Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String variable
On Sun, 2009-01-11 at 14:36 +, Ashley Sheridan wrote: On Sun, 2009-01-11 at 08:59 -0500, MikeP wrote: Hello, I am trying yo get THIS: where ref_id = '1234' from this. $where=where ref_id=.'$Reference[$x][ref_id]'; but i certainly have a quote problem. Any help? Thanks Mike It should look like this: $where=where ref_id='{$Reference[$x][ref_id]}'; Ash www.ashleysheridan.co.uk Sorry, it should look like this: $where=where ref_id='{$Reference[$x][ref_id]}'; I missed taking an extra quote mark out Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String variable
Ashley Sheridan wrote: On Sun, 2009-01-11 at 14:36 +, Ashley Sheridan wrote: On Sun, 2009-01-11 at 08:59 -0500, MikeP wrote: Hello, I am trying yo get THIS: where ref_id = '1234' from this. $where=where ref_id=.'$Reference[$x][ref_id]'; but i certainly have a quote problem. Any help? Thanks Mike It should look like this: $where=where ref_id='{$Reference[$x][ref_id]}'; Ash www.ashleysheridan.co.uk Sorry, it should look like this: $where=where ref_id='{$Reference[$x][ref_id]}'; I missed taking an extra quote mark out Ash www.ashleysheridan.co.uk actually unless ref_id is a constant (which i doublt) you may be best going with: $where = WHERE ref_id=' . $Reference[$x]['ref_id'] . '; keep the php and sql seperate and you'll find it much easier to see in you're editor (imho) :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String variable
2009/1/11 Ashley Sheridan a...@ashleysheridan.co.uk: On Sun, 2009-01-11 at 14:36 +, Ashley Sheridan wrote: On Sun, 2009-01-11 at 08:59 -0500, MikeP wrote: Hello, I am trying yo get THIS: where ref_id = '1234' from this. $where=where ref_id=.'$Reference[$x][ref_id]'; but i certainly have a quote problem. Any help? Thanks Mike It should look like this: $where=where ref_id='{$Reference[$x][ref_id]}'; Ash www.ashleysheridan.co.uk Sorry, it should look like this: $where=where ref_id='{$Reference[$x][ref_id]}'; I missed taking an extra quote mark out Closer, but still not quite there. For encapsulation in the string, it should look like: $where = where ref_is='{$Reference[$x]['ref_id']}'; Someone else mentioned casting to int first as well to sanitize, which is also a good idea. Torben Ash www.ashleysheridan.co.uk -- Torben Wilson tor...@2powerweb.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string comparison
On Sun, 2008-07-13 at 21:47 +0530, Sudhakar wrote: hi i am writing a small application where a user enters a phrase in the textfield and i would like to display all the files present in the root directory which consists of the keyword or keywords entered by the user. i have used a few comparison functions but i am not getting the expected result. $my_file = file_get_contents(filename.html); what ever the user enters whether it is a single word or few words i would like to compare with $my_file in a case insensitive manner. can anyone suggest the best method and how to go about. I don't suggest using file_get_contents. It would probably be more efficient (at least less memory intensive) to use fopen() and fread(). Just be sure you overlap each read by $the_size_of_the_largest phrase_or_keyword - 1. Then use stripos() for matching... of course that won't work so well if whitespace doesn't need to match exactly in phrases. In which case you'll need to resort to other techniques. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string comparison
On Jul 13, 2008, at 9:17 AM, Sudhakar wrote: hi i am writing a small application where a user enters a phrase in the textfield and i would like to display all the files present in the root directory which consists of the keyword or keywords entered by the user. i have used a few comparison functions but i am not getting the expected result. I use this script to list archive files from a directory based on keyword. I'd guess a modified version using the keywords from users might work: // create archives box if ($handle = opendir('../diaryarchives/')) { while (false !== ($file = readdir($handle))) { $pos = strpos($file, diary_); $pagemarked = diary._; if ($pos !== false) { //print $filebr; $file_name = ereg_replace ($pagemarked,,$file); $file_name = ereg_replace (.php,,$file_name); //print * $file_namebr; //print $filebr; $archive_list_gather[] = 'lia href=/diaryarchives/'. $file.''.$file_name.'/a/li'; } } closedir($handle); } rsort($archive_list_gather); foreach($archive_list_gather as $value) { $archive_list .= $value; } // build archives box $archives_box = 'div id=diary-archives h3 class=sideimg src=/images/h3s_diaryarchives.gif alt=Diary Archives width=225 height=20 //h3 ul '.$archive_list.' /ul /div'; // publish archives box $filename = PATHA.'/diaryarchivesbox.php'; publishpages($archives_box, $filename); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String to date
On Mon, Jun 30, 2008 at 4:58 PM, Mark Bomgardner [EMAIL PROTECTED] wrote: I need to convert a date retrieved from user input to a mysql date. Here the problem, I need to convert one of three possible combinations, either 01/01/2008,01-01-2008 or 01.01.2008. I can't use explode because it's limited to one character to explode on. I would prefer not to use regexp, but think I am going to have to. The one part of the code that works below is using 01/01/2008 format. Any suggestions echo $olddate = '06/06/2008'; echo br /; echo $olddate2 = '06-16-2008'; echo br /; echo $olddate3 = '06.26.2008'; echo br /; echo $newdate = date(Y-m-d,strtotime($olddate)); echo br /; echo $newdate2 = date(Y-m-d,strtotime($olddate2)); echo br /; echo $newdate3 = date(Y-m-d,strtotime($olddate3)); markb -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Your date field should always be the same in the database. The just use the date function to format that date when displaying to the user...don't alter the date format as it may make the date field unusable for queries or throw errors when attempting to insert. -- Bastien Cat, the other other white meat
Re: [PHP] String to date
Mark Bomgardner [EMAIL PROTECTED] wrote: I need to convert a date retrieved from user input to a mysql date. Here the problem, I need to convert one of three possible combinations, either 01/01/2008,01-01-2008 or 01.01.2008. I can't use explode because it's limited to one character to explode on. I would prefer not to use regexp, but think I am going to have to. The one part of the code that works below is using 01/01/2008 format. Any suggestions echo $olddate = '06/06/2008'; echo br /; echo $olddate2 = '06-16-2008'; echo br /; echo $olddate3 = '06.26.2008'; echo br /; echo $newdate = date(Y-m-d,strtotime($olddate)); echo br /; echo $newdate2 = date(Y-m-d,strtotime($olddate2)); echo br /; echo $newdate3 = date(Y-m-d,strtotime($olddate3)); markb You've given us no code you are actually using (we can all write dummy test code). IMO, you need to either change your input form to give you the results in a certain way (split up the M,D,Y or only accept it in a specific format or any other way) OR You run the strpos and look for / . or - or or ? and then use the data on that field. Wolf -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] String to date
-Original Message- From: Mark Bomgardner [mailto:[EMAIL PROTECTED] Sent: Monday, June 30, 2008 3:58 PM To: php-general@lists.php.net Subject: [PHP] String to date I need to convert a date retrieved from user input to a mysql date. Here the problem, I need to convert one of three possible combinations, either 01/01/2008,01-01-2008 or 01.01.2008. I can't use explode because it's limited to one character to explode on. I would prefer not to use regexp, but think I am going to have to. The one part of the code that works below is using 01/01/2008 format. Any suggestions echo $olddate = '06/06/2008'; echo br /; echo $olddate2 = '06-16-2008'; echo br /; echo $olddate3 = '06.26.2008'; echo br /; echo $newdate = date(Y-m-d,strtotime($olddate)); echo br /; echo $newdate2 = date(Y-m-d,strtotime($olddate2)); echo br /; echo $newdate3 = date(Y-m-d,strtotime($olddate3)); Step 1.) Replace all - with /. Step 2.) Replace all . with /. Step 3.) Err.. wait.. you're done. Todd Boyd Web Programmer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String to date
couldn't strtotime() do this without any mods? I personally would try that first... On 6/30/08, Mark Bomgardner [EMAIL PROTECTED] wrote: I need to convert a date retrieved from user input to a mysql date. Here the problem, I need to convert one of three possible combinations, either 01/01/2008,01-01-2008 or 01.01.2008. I can't use explode because it's limited to one character to explode on. I would prefer not to use regexp, but think I am going to have to. The one part of the code that works below is using 01/01/2008 format. Any suggestions echo $olddate = '06/06/2008'; echo br /; echo $olddate2 = '06-16-2008'; echo br /; echo $olddate3 = '06.26.2008'; echo br /; echo $newdate = date(Y-m-d,strtotime($olddate)); echo br /; echo $newdate2 = date(Y-m-d,strtotime($olddate2)); echo br /; echo $newdate3 = date(Y-m-d,strtotime($olddate3)); markb -- 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
Re: [PHP] String searching
On Sat, May 17, 2008 at 2:17 AM, Chris W [EMAIL PROTECTED] wrote: I need to find the position of the first character in the string (searching from the end) that is not one of the characters in a set. In this case the set is [0-9a-zA-z-_] To find the position of a specific character, RTFM on strpos(). For those not existing in your condition, I'd recommend everythingbut(), but it's yet to be included in the core. ;-P I guess to be even more specific, I want to split a string into to parts the first part can contain anything and the second part must be only in the set described above. You can split a string by doing something as simple as this: ?php $str = abcdefghijklmnopqrstuvwxyz; $d = $str[5]; // $d == position - 1, because count always begins with 0 ? So to walk backward through the string, while it's not very clean, you could do: ?php $str = ABCDEF01234567789; for($i=strlen($str);$i0;$i--) { if(preg_match('/[g-z]/i',$str[$i])) { // Handle your this is a bad character condition(s). // break; /* Or, optionally, continue. */ } } ? Not pretty, but if my mind is still working at 2:30a (EDT), it should help you out. -- /Daniel P. Brown Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just $59.99/mo. with no contract! Dedicated servers, VPS, and hosting from $2.50/mo. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String searching
Chris W wrote: I need to find the position of the first character in the string (searching from the end) that is not one of the characters in a set. In this case the set is [0-9a-zA-z-_] I guess to be even more specific, I want to split a string into to parts the first part can contain anything and the second part must be only in the set described above. What is the easiest way to do this? There's something here, imaginatively called blah(), which does what you require: http://www.phpguru.org/preg/example.phps -- Richard Heyes ++ | Access SSH with a Windows mapped drive | |http://www.phpguru.org/sftpdrive| ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string
John Taylor-Johnston wrote: $name = John Taylor; I want to verify if $name contains john, if yes echo found; Cannot remember which to use: http://ca.php.net/manual/en/ref.strings.php Either http://php.net/strpos or http://php.net/stripos if your version of PHP supports it. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] string
Do a preg match to find one or preg_match_all to find all the john in the string. ?php $name = John Taylor; $pattern = '/^John/'; preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3); print_r($matches); ? $name = John Taylor; I want to verify if $name contains john, if yes echo found; Cannot remember which to use: http://ca.php.net/manual/en/ref.strings.php Sorry, John -- 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
Re: [PHP] string
On Mon, Apr 7, 2008 at 9:25 AM, John Taylor-Johnston [EMAIL PROTECTED] wrote: $name = John Taylor; I want to verify if $name contains john, if yes echo found; Cannot remember which to use: http://ca.php.net/manual/en/ref.strings.php Sorry, John ?php if(stristr($name,'john')) { // True } ? Since you said you wanted to know if it contains 'john', this will find if 'john' (insensitive) matches any part of the name. So in your case, it would match on both John and Johnston. -- /Daniel P. Brown Ask me about: Dedicated servers starting @ $59.99/mo., VPS starting @ $19.99/mo., and shared hosting starting @ $2.50/mo. Unmanaged, managed, and fully-managed! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string
Excellent. Thanks all! John Daniel Brown wrote: On Mon, Apr 7, 2008 at 9:25 AM, John Taylor-Johnston [EMAIL PROTECTED] wrote: $name = John Taylor; I want to verify if $name contains john, if yes echo found; Cannot remember which to use: http://ca.php.net/manual/en/ref.strings.php Sorry, John ?php if(stristr($name,'john')) { // True } ? Since you said you wanted to know if it contains 'john', this will find if 'john' (insensitive) matches any part of the name. So in your case, it would match on both John and Johnston.
Re: [PHP] string
On Mon, Apr 7, 2008 at 9:30 AM, [EMAIL PROTECTED] wrote: Do a preg match to find one or preg_match_all to find all the john in the string. preg_* is overkill if you're just searching for a literal string. use it if you're searching for any strings matching a pattern, part of which you don't know. If you know the entire string you're looking for, strstr() is both more efficient and easier to use. Now, strpos() is more efficient still, but arguably more annoying to use because of the 0 but true issue that necessitates checking for !== false. Besides efficiency, the only difference between strstr() and strpos() is what they return. strpos() returns the index of the first match, while strstr() returns the entire string starting from that point; it's the building of the copy of the string that causes strstr() to be less efficient. Both functions have case-insensitive variants stristr and stripos. In each case, if the substring occurs more than once within the outer string, the return value is based on the *first* occurrence. strpos() (but not strstr()) has a variant that uses the *last* one instead: strrpos() (r=reverse), which also has a case-insensitive version strripos(). You can easily define a strrstr() though: function strrstr($where, $what) { $pos = strrpos($where, $what); return $pos === false ? false : substr($where, $pos); } And for good measure, a strristr: function strristr($where, $what) { $pos = strripos($where, $what); return $pos === false ? false : substr($where, $pos); } ?php $name = John Taylor; $pattern = '/^John/'; preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3); print_r($matches); ? $name = John Taylor; I want to verify if $name contains john, if yes echo found; Cannot remember which to use: http://ca.php.net/manual/en/ref.strings.php Sorry, John -- 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 -- Mark J. Reed [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string
Daniel Brown wrote: On Mon, Apr 7, 2008 at 9:25 AM, John Taylor-Johnston [EMAIL PROTECTED] wrote: $name = John Taylor; I want to verify if $name contains john, if yes echo found; Cannot remember which to use: http://ca.php.net/manual/en/ref.strings.php Sorry, John ?php if(stristr($name,'john')) { // True } ? Since you said you wanted to know if it contains 'john', this will find if 'john' (insensitive) matches any part of the name. So in your case, it would match on both John and Johnston. I wonder if using strstr() with strtolower() would be faster or slower. ?php $max = 10; $it = 0; while ( $it++ $max ) { echo Attempt #{$it}br /; $string = 'John Doe'; $counter = 1; $new_string = strtolower($string); $start_time_1 = microtime(true); while ( $counter-- ) { if ( strstr($new_string, 'john') ) { ### Found it } } echo microtime(true) - $start_time_1.br /; $counter = 1; $start_time = microtime(true); while ( $counter-- ) { if ( stristr($string, 'john') ) { ### Found it } } echo microtime(true) - $start_time.br /; } ? Results: Attempt #1 0.015728950500488 0.022881031036377 Attempt #2 0.015363931655884 0.022166967391968 Attempt #3 0.015865087509155 0.022243022918701 Attempt #4 0.015905857086182 0.022934198379517 Attempt #5 0.015322208404541 0.022816181182861 Attempt #6 0.015490055084229 0.021909952163696 Attempt #7 0.015805959701538 0.021935939788818 Attempt #8 0.01572585105896 0.022881984710693 Attempt #9 0.015491008758545 0.022812128067017 Attempt #10 0.015367031097412 0.02212119102478 -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string
On Mon, Apr 7, 2008 at 10:42 AM, Jim Lucas [EMAIL PROTECTED] wrote: I wonder if using strstr() with strtolower() would be faster or slower. [snip=code] Results: Attempt #1 0.015728950500488 0.022881031036377 [snip!] While I don't really care much for a single selection about the difference of less than 7/1,000's of one second, that's still good information to keep in mind - especially when dealing with large-scale sites. Nice work, Jim. -- /Daniel P. Brown Ask me about: Dedicated servers starting @ $59.99/mo., VPS starting @ $19.99/mo., and shared hosting starting @ $2.50/mo. Unmanaged, managed, and fully-managed! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string effect
At 9:51 PM +0100 2/29/08, Alain Roger wrote: What is the basic rule ? Text is cut off based on (numbers of words, number of characters,..) ? Yes. Use whatever you want. You can use the number characters or find the last *space* in a string that's just long enough to fit your limit. Let's say your limit is 100 characters. 1. First truncate the string to 100 characters. 2. Then search the string for the last space. 3. Then truncate the string at that point and add It will be a good exercise for you. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string effect
Mr. Heyes more or less prompted me to go dig for my other, slightly heavier version, that doesn't chop words up: Sorry I hit Reply instead Reply All. Regardless, here's my str_curtail. There is a bug in it that means if the string is all one word then it gets curtailed to nada, but that's easily fixed. /** * Shortens the given string to the specified number of characters, * however will never shorten mid-word (going backwards to find white * space). Appends * ... (unless third arg is given). * * @param string $strInput to shorten * @param int$length Length to shorten to (defaults to 35) * @param string $append String to append (defaults to ...) * @return string Resulting shortened string */ function str_curtail($str, $length = 35, $append = '...') { // String short enough already ? if (strlen($str) = $length) { return $str; } $str = substr($str, 0, $length); // No body intentionally for ($i=$length - 1; !ctype_space($str{$i}) $i 0; --$i); return rtrim(substr($str, 0, $i)) . $append; } -- Richard Heyes Employ me: http://www.phpguru.org/cv -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string effect
On 2/29/08, Alain Roger [EMAIL PROTECTED] wrote: Hi, since a long time now, i see that some paragraphs of text are cut off and the additional text is replaced by 3 dots. e.g: this is the original long text but without any sense and also stupid effect desired : this is the original long text... this is usually used for title articles or some long paragraphs. What is the basic rule ? Text is cut off based on (numbers of words, number of characters,..) ? what is the algorithm for such thing ? thanks a lot, function truncate( $string, $length=384, $ending='...' ) { if( strlen( $string ) $length ) $string = substr( $string, 0, $length ) . $ending ; return $string; } -- Greg Donald http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string effect
On 2/29/08, Alain Roger [EMAIL PROTECTED] wrote: Text is cut off based on (numbers of words, number of characters,..) ? what is the algorithm for such thing ? Mr. Heyes more or less prompted me to go dig for my other, slightly heavier version, that doesn't chop words up: function breakUpLongLines( $text, $maxLength=96 ) { $counter = 0; $newText = ''; $array = array(); $textLength = strlen( $text ); for( $i = 0; $i = $textLength; $i++ ) { $array[] = substr( $text, $i, 1 ); } $textLength = count( $array ); for( $x = 0; $x $textLength; $x++ ) { if( preg_match( /[[:space:]]/, $array[ $x ] ) ) { $counter = 0; } else { $counter++; } $newText .= $array[ $x ]; if( $counter = $maxLength ) { $newText .= ' '; $counter = 0; } } return $newText; } -- Greg Donald http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] string vs number
On 05 February 2008 21:37, Jochem Maas advised: the same is not exactly true for floats - although you can use them as array keys you'll notice in the output of code below that they are stripped of their decimal part (essentially a floor() seems to be performed on the float value. I have no idea whether this is intentional, and whether you can therefore rely on this behaviour: Yes, and Yes! From http://php.net/language.types.array: A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. 8 will be interpreted as 8, while 08 will be interpreted as 08). Floats in key are truncated to integer. -- Mike Ford, Electronic Information Services Adviser, JG125, The Headingley Library, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 812 4730 Fax: +44 113 812 3211 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string vs number
Ford, Mike schreef: On 05 February 2008 21:37, Jochem Maas advised: the same is not exactly true for floats - although you can use them as array keys you'll notice in the output of code below that they are stripped of their decimal part (essentially a floor() seems to be performed on the float value. I have no idea whether this is intentional, and whether you can therefore rely on this behaviour: Yes, and Yes! From http://php.net/language.types.array ah yes, I should have looked it up, that said I find it rather odd that is works let alone that it's intentional. though thinking about it you could probably use it for some float val distribution counting or something. I dunno, seems like it offers a handy shortcut - although what that shortcut is escapes me just now :-) A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. 8 will be interpreted as 8, while 08 will be interpreted as 08). Floats in key are truncated to integer. -- Mike Ford, Electronic Information Services Adviser, JG125, The Headingley Library, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 812 4730 Fax: +44 113 812 3211 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string vs number
On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote: hi all, i have this php statement: ? if($rowB[$rowA[0]]=='Y') {echo checked;} ? debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB['54']. is this possible? how do i force $rowA[0] to be a string ('54')?http://www.php.net/unsub.php php should handle the conversion internally for you. if you want to type cast a value to a string, simply do (string)$varname -nathan
Re: [PHP] string vs number
On Feb 5, 2008 1:40 PM, Nathan Nobbe [EMAIL PROTECTED] wrote: On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote: hi all, i have this php statement: ? if($rowB[$rowA[0]]=='Y') {echo checked;} ? debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB['54']. is this possible? how do i force $rowA[0] to be a string ('54')?http://www.php.net/unsub.php php should handle the conversion internally for you. if you want to type cast a value to a string, simply do (string)$varname -nathan I was thinking about saying that, but php is loosely typed, so 54 == '54'. I'm thinking something else is wrong here. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string vs number
On Feb 5, 2008 1:43 PM, Eric Butera [EMAIL PROTECTED] wrote: I was thinking about saying that, but php is loosely typed, so 54 == '54'. i only mentioned the type cast because it was asked about; actually, there are rare times in php when type casts are called for, such as pulling a value from a SimpleXMLElement. but that is neither here nor there.. I'm thinking something else is wrong here. ya, like $rowB['54'] != 'Y'; // ;) -nathan
Re: [PHP] string vs number
On Feb 5, 2008, at 10:43 AM, Eric Butera [EMAIL PROTECTED] wrote: On Feb 5, 2008 1:40 PM, Nathan Nobbe [EMAIL PROTECTED] wrote: On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote: hi all, i have this php statement: ? if($rowB[$rowA[0]]=='Y') {echo checked;} ? debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB ['54']. is this possible? how do i force $rowA[0] to be a string ('54')?http://www.php.net/unsub.php php should handle the conversion internally for you. if you want to type cast a value to a string, simply do (string)$varname -nathan I was thinking about saying that, but php is loosely typed, so 54 == '54'. I'm thinking something else is wrong here. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php I believe this is the difference with arrays: $a = array(2 = foo); Array(0 = null, 1 = null, 2 = foo) $a = array(2 = foo); Array(2 = foo) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string vs number
On Feb 5, 2008 1:50 PM, Casey [EMAIL PROTECTED] wrote: I believe this is the difference with arrays: $a = array(2 = foo); Array(0 = null, 1 = null, 2 = foo) $a = array(2 = foo); Array(2 = foo) i think the implicit type casting applies there as well: php $meh = array(2=4); php echo $meh['2']; 4 php $meh['2'] = 5; php echo $meh['2']; 5 php echo $meh[2]; 5 -nathan
Re: [PHP] string vs number
On Feb 5, 2008 1:48 PM, Nathan Nobbe [EMAIL PROTECTED] wrote: On Feb 5, 2008 1:43 PM, Eric Butera [EMAIL PROTECTED] wrote: I was thinking about saying that, but php is loosely typed, so 54 == '54'. i only mentioned the type cast because it was asked about; actually, there are rare times in php when type casts are called for, such as pulling a value from a SimpleXMLElement. but that is neither here nor there.. I'm thinking something else is wrong here. ya, like $rowB['54'] != 'Y'; // ;) -nathan Yep, I use them all the time. I just meant that I wasn't sure this is what was going to get the OP fixed. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string vs number
On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote: hi all, i have this php statement: ? if($rowB[$rowA[0]]=='Y') {echo checked;} ? debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB['54']. is this possible? how do i force $rowA[0] to be a string ('54')? Type casting shouldn't be an issue in this case. For example, you're not trying to convert alphanumeric characters to int() (which would actually go to boolean - 0/1), so when trying this case, the condition is True: ? $rowA[] = 54; $rowA[] = 63; $rowA[] = 72; $rowB['54'] = Y; $rowB['63'] = N; $rowB['72'] = N; if($rowB[$rowA[0]]=='Y') { echo checked.\n; } ? Because of the loose-typecasting nature of PHP (done on purpose), '54' does, in fact, equal 54, unless otherwise specifically stated. -- /Dan Daniel P. Brown Senior Unix Geek ? while(1) { $me = $mind--; sleep(86400); } ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string vs number
Casey schreef: On Feb 5, 2008, at 10:43 AM, Eric Butera [EMAIL PROTECTED] wrote: On Feb 5, 2008 1:40 PM, Nathan Nobbe [EMAIL PROTECTED] wrote: On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote: hi all, i have this php statement: ? if($rowB[$rowA[0]]=='Y') {echo checked;} ? debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB['54']. is this possible? how do i force $rowA[0] to be a string ('54')?http://www.php.net/unsub.php php should handle the conversion internally for you. if you want to type cast a value to a string, simply do (string)$varname -nathan I was thinking about saying that, but php is loosely typed, so 54 == '54'. I'm thinking something else is wrong here. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php I believe this is the difference with arrays: $a = array(2 = foo); Array(0 = null, 1 = null, 2 = foo) $a = array(2 = foo); Array(2 = foo) not true: alice:~ jochem$ php -r ' $a = array(2 = foo); $b = array(2 = foo); var_dump($a, $b);' array(1) { [2]= string(3) foo } array(1) { [2]= string(3) foo } php treats anything that is the string equivelant of an integer as an integer when it comes to array keys - which comes down to the fact that you cannot therefore use a string version of an integer as an associative key. so $a[2] and $a[2] are always the same element. the same is not exactly true for floats - although you can use them as array keys you'll notice in the output of code below that they are stripped of their decimal part (essentially a floor() seems to be performed on the float value. I have no idea whether this is intentional, and whether you can therefore rely on this behaviour: alice:~ jochem$ php -r ' $a = array(2.5 = foo); $b = array(2.5 = foo); var_dump($a, $b);' array(1) { [2]= string(3) foo } array(1) { [2.5]= string(3) foo } alice:~ jochem$ php -r ' $a = array(2.6 = foo); $b = array(2.6 = foo); var_dump($a, $b);' array(1) { [2]= string(3) foo } array(1) { [2.6]= string(3) foo } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Issue
On Wed, 2008-01-23 at 23:30 -0600, Johny Burns wrote: I have the following string on the address line HTMLFiles/MenuDisplay.php?var=Thai%20ImageItem=1797Action=add I am trying to delete or replace the 'Item=1797Action=add' (it is at the end of the string) I am not familiar as much with those string functions, and if somebody can give me some suggestions. I will appreciated it. Thank you in advance. You want the following functions: http://www.php.net/manual/en/function.parse-url.php http://www.php.net/manual/en/function.parse-str.php Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] String Issue
Hi, Try this: $str = 'HTMLFiles/MenuDisplay.php?var=Thai%20ImageItem=1797Action=add'; $str = preg_replace(/(\Item.*)$/,REPLACEMENT STRING, $str); this should work. Cheers, V -Original Message- From: Johny Burns [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Thu, 24 Jan 2008 11:00 am Subject: [PHP] String Issue I have the following string on the address line HTMLFiles/MenuDisplay.php?var=Thai%20ImageItem=1797Action=add I am trying to delete or replace the 'Item=1797Action=add' (it is at the end of the string) I am not familiar as much with those string functions, and if somebody can give me some suggestions. I will appreciated it. Thank you in advance. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php You are invited to Get a Free AOL Email ID. - http://webmail.aol.in
Re: [PHP] string as file
Robert Cummings wrote: On Fri, 2007-08-10 at 16:28 +0100, Stut wrote: Rick Pasotto wrote: On Fri, Aug 10, 2007 at 02:19:29PM +0100, Stut wrote: Rick Pasotto wrote: On Thu, Aug 09, 2007 at 03:25:27PM -0500, Greg Donald wrote: On 8/9/07, Rick Pasotto [EMAIL PROTECTED] wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. eval() Explain. One word responses really don't do any good. Exactly *what* would be the argument to eval()? RTFM, that's what it's there for. I did. That's why I rejected the use of eval() before I posted the message. eval() is totally unsuitable for what I want. Unless, that is, you or Greg can explain how using eval() will get me what I want. I think that neither you nor Greg understands what I'm looking for. Instead of simply stating 'RTFM' perhaps *you* should RTFQuestion. Your original post asked... Can I someout include a string instead of a file? That's exactly what eval does. As for what you would pass to it... PHP code maybe? Have you even tried it? The manual page for eval has several examples of how to use it, and the comments have even more. Incidentally, eval is evil and potentially a giant security hole. You'd be better off doing replacements with preg_match rather than executing a string. Agreed. That's another reason I had already rejected it. Although in this case, since I would have full control of all the variables, it would probably be ok. Use regular expressions or straight string replacements - that's the best way to implement mail-merge type behaviour. Personally I used preg_replace with the 'e' modifier. For an example see, shockingly, the manual page for preg_replace. Now go stick your head in a pig. Spider-Pig, Spider-Pig, does whatever a Spider-Pig does... :) I love Fridays!! It's Friday? Gawdammit! -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
On Fri, 2007-08-10 at 16:28 +0100, Stut wrote: Rick Pasotto wrote: On Fri, Aug 10, 2007 at 02:19:29PM +0100, Stut wrote: Rick Pasotto wrote: On Thu, Aug 09, 2007 at 03:25:27PM -0500, Greg Donald wrote: On 8/9/07, Rick Pasotto [EMAIL PROTECTED] wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. eval() Explain. One word responses really don't do any good. Exactly *what* would be the argument to eval()? RTFM, that's what it's there for. I did. That's why I rejected the use of eval() before I posted the message. eval() is totally unsuitable for what I want. Unless, that is, you or Greg can explain how using eval() will get me what I want. I think that neither you nor Greg understands what I'm looking for. Instead of simply stating 'RTFM' perhaps *you* should RTFQuestion. Your original post asked... Can I someout include a string instead of a file? That's exactly what eval does. As for what you would pass to it... PHP code maybe? Have you even tried it? The manual page for eval has several examples of how to use it, and the comments have even more. Incidentally, eval is evil and potentially a giant security hole. You'd be better off doing replacements with preg_match rather than executing a string. Agreed. That's another reason I had already rejected it. Although in this case, since I would have full control of all the variables, it would probably be ok. Use regular expressions or straight string replacements - that's the best way to implement mail-merge type behaviour. Personally I used preg_replace with the 'e' modifier. For an example see, shockingly, the manual page for preg_replace. Now go stick your head in a pig. Spider-Pig, Spider-Pig, does whatever a Spider-Pig does... :) I love Fridays!! Cheers, Rob. -- ... SwarmBuy.com - http://www.swarmbuy.com Leveraging the buying power of the masses! ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
Rick Pasotto wrote: On Thu, Aug 09, 2007 at 02:39:51PM -0700, Jim Lucas wrote: Rick Pasotto wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. Can I someout include a string instead of a file? Or maybe there is some completely different way to do what I want. template.php ?php ob_start(); echo Hi, my name is {$first_name} {$last_name}.; return ob_get_clean(); ? This is two different ways you can do it, bases on your input data array structure test.php ?php $string = 'Hi, my name is |FIRST_NAME| |LAST_NAME|.'; $values = array(); $values[] = array('FIRST_NAME' = 'Jim', 'LAST_NAME' = 'Lucas'); $values[] = array('FIRST_NAME' = 'James', 'LAST_NAME' = 'Lucas'); $values[] = array('FIRST_NAME' = 'Jimmy', 'LAST_NAME' = 'Lucas'); foreach ( $values AS $row ) { $in = array_keys($row); foreach ( $in AS $k = $v ) $in[$k] = '|'.$v.'|'; echo str_replace($in, $row, $string); } ? You have misunderstood. You are still putting the template in an external file. I want it in the main file. I don't want to maintain two different files. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
Rick Pasotto wrote: On Fri, Aug 10, 2007 at 02:19:29PM +0100, Stut wrote: Rick Pasotto wrote: On Thu, Aug 09, 2007 at 03:25:27PM -0500, Greg Donald wrote: On 8/9/07, Rick Pasotto [EMAIL PROTECTED] wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. eval() Explain. One word responses really don't do any good. Exactly *what* would be the argument to eval()? RTFM, that's what it's there for. I did. That's why I rejected the use of eval() before I posted the message. eval() is totally unsuitable for what I want. Unless, that is, you or Greg can explain how using eval() will get me what I want. I think that neither you nor Greg understands what I'm looking for. Instead of simply stating 'RTFM' perhaps *you* should RTFQuestion. Your original post asked... Can I someout include a string instead of a file? That's exactly what eval does. As for what you would pass to it... PHP code maybe? Have you even tried it? The manual page for eval has several examples of how to use it, and the comments have even more. Incidentally, eval is evil and potentially a giant security hole. You'd be better off doing replacements with preg_match rather than executing a string. Agreed. That's another reason I had already rejected it. Although in this case, since I would have full control of all the variables, it would probably be ok. Use regular expressions or straight string replacements - that's the best way to implement mail-merge type behaviour. Personally I used preg_replace with the 'e' modifier. For an example see, shockingly, the manual page for preg_replace. Now go stick your head in a pig. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
On Fri, Aug 10, 2007 at 02:19:29PM +0100, Stut wrote: Rick Pasotto wrote: On Thu, Aug 09, 2007 at 03:25:27PM -0500, Greg Donald wrote: On 8/9/07, Rick Pasotto [EMAIL PROTECTED] wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. eval() Explain. One word responses really don't do any good. Exactly *what* would be the argument to eval()? RTFM, that's what it's there for. I did. That's why I rejected the use of eval() before I posted the message. eval() is totally unsuitable for what I want. Unless, that is, you or Greg can explain how using eval() will get me what I want. I think that neither you nor Greg understands what I'm looking for. Instead of simply stating 'RTFM' perhaps *you* should RTFQuestion. Incidentally, eval is evil and potentially a giant security hole. You'd be better off doing replacements with preg_match rather than executing a string. Agreed. That's another reason I had already rejected it. Although in this case, since I would have full control of all the variables, it would probably be ok. -- Whatever crushes individuality is despotism, by whatever name it may be called. -- John Stuart Mill, 1859 Rick Pasotto[EMAIL PROTECTED]http://www.niof.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
On Thu, Aug 09, 2007 at 02:39:51PM -0700, Jim Lucas wrote: Rick Pasotto wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. Can I someout include a string instead of a file? Or maybe there is some completely different way to do what I want. template.php ?php ob_start(); echo Hi, my name is {$first_name} {$last_name}.; return ob_get_clean(); ? This is two different ways you can do it, bases on your input data array structure test.php ?php $values = array(); $values[] = array('first_name' = 'Jim','last_name' = 'Lucas'); $values[] = array('first_name' = 'James','last_name' = 'Lucas'); $values[] = array('first_name' = 'Jimmy','last_name' = 'Lucas'); foreach ($values AS $row) { extract($row); echo include 'template.php'; } $values = array(); $values[] = array('Jim','Lucas'); $values[] = array('James','Lucas'); $values[] = array('Jimmy','Lucas'); list($first_name, $last_name) = current($values); do { echo include 'template.php'; } while (list($first_name, $last_name) = next($values)); ? You have misunderstood. You are still putting the template in an external file. I want it in the main file. I don't want to maintain two different files. -- It is always from a minority acting in ways different from what the majority would prescribe that the majority in the end learns to do better. -- Friedrich Hayek Rick Pasotto[EMAIL PROTECTED]http://www.niof.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
On Thu, Aug 09, 2007 at 03:25:27PM -0500, Greg Donald wrote: On 8/9/07, Rick Pasotto [EMAIL PROTECTED] wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. eval() Explain. One word responses really don't do any good. Exactly *what* would be the argument to eval()? -- In vices, the very essence of crime -- that is, the design to injure the person or property of another -- is wanting. It is a maxim of law that there can be no crime without a criminal intent. -- Lysander Spooner Rick Pasotto[EMAIL PROTECTED]http://www.niof.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
Rick Pasotto wrote: On Thu, Aug 09, 2007 at 03:25:27PM -0500, Greg Donald wrote: On 8/9/07, Rick Pasotto [EMAIL PROTECTED] wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. eval() Explain. One word responses really don't do any good. Exactly *what* would be the argument to eval()? RTFM, that's what it's there for. Incidentally, eval is evil and potentially a giant security hole. You'd be better off doing replacements with preg_match rather than executing a string. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
On 8/9/07, Rick Pasotto [EMAIL PROTECTED] wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. eval() -- Greg Donald http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] string as file
Rick Pasotto wrote: Does php have a facility similar to python's stringIO? What I'm wanting to do is similar to a mail merge. IOW, I know I can create an include file like: $out = EOT This is an example of $var1 and $var2. EOT; and then after assigning values to $var1 and $var2 include that file. I can later use different values for $var1 and $var2 and get a different $out with a second include. Can I someout include a string instead of a file? Or maybe there is some completely different way to do what I want. template.php ?php ob_start(); echo Hi, my name is {$first_name} {$last_name}.; return ob_get_clean(); ? This is two different ways you can do it, bases on your input data array structure test.php ?php $values = array(); $values[] = array('first_name' = 'Jim','last_name' = 'Lucas'); $values[] = array('first_name' = 'James','last_name' = 'Lucas'); $values[] = array('first_name' = 'Jimmy','last_name' = 'Lucas'); foreach ($values AS $row) { extract($row); echo include 'template.php'; } $values = array(); $values[] = array('Jim','Lucas'); $values[] = array('James','Lucas'); $values[] = array('Jimmy','Lucas'); list($first_name, $last_name) = current($values); do { echo include 'template.php'; } while (list($first_name, $last_name) = next($values)); ? -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] string problem
[snip] I have the SQL statement which store in $str variable as shown below : How to get the '%WS-X5225R%' from $str variable ? $str = SELECT DISTINCT(tbl_chassis.serial_no),tbl_card.card_model, ; tbl_chassis.host_name,tbl_chassis.chasis_model,tbl_chassis.country,; tbl_chassis.city,tbl_chassis.building, tbl_chassis.other,tbl_chassis.status,tbl_chassis.chasis_eos,tbl_chassis .chasis_eol,tbl_chassis.chasis_user_field_1,tbl_chassis.chasis_user_fiel d_2,tbl_chassis.chasis_user_field_3 from tbl_chassis tbl_chassis,tbl_card tbl_card WHERE tbl_chassis.serial_no = tbl_card.serial_no AND tbl_card.card_model LIKE'%WS-X5225R%'; [/snip] What do you mean get the '%WS-X5225R%' from $str variable ? Do you want to be able to change that value? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] STRING TO ASCII CHARACTERS
Ahmed Saad wrote: On 23/06/06, cajbecu [EMAIL PROTECTED] wrote: $data .= #.ord(substr($string,$i,1)).;; and I think there's no need for substr.. just $data .= #.$string[$i].;; /ahmed And, for the record, you are not converting a string to ASCII, rather an ASCII string to its decimal equivalent. -J -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php