Corey 0 Report post Posted May 29, 2005 i have 2 questions about PHP...please don't answer like "use google"...or something...because i tried and didn't find anything..so1.example..i have a varible called $var and it contains 50 characters..so how can i write only part of them (1,2,3,20)?2.example like the one before... i have a varible called $var with 50 characters..how can i write the first /second /third.. charachter from the $vartnx;) Share this post Link to post Share on other sites
OpaQue 15 Report post Posted May 29, 2005 The substr function of PHP will allow you to get part of the variable.The access particular characters, you will have to use a Curly braces.Suppose, you have $var="tommy";So, $var{1} will give you "o" as output. This is a rough idea since I haven't used this function for a long time.However, you can search for substr directly now :-) instead of searching everything. Share this post Link to post Share on other sites
Tyssen 0 Report post Posted May 30, 2005 You can also specify the number of characters and count from the begining or end of the string. PHP.net has some good examples of how that works. But I think you can only use substr if the characters run in succession, e.g. 1-3, 4-8 etc. If you want to extract characters at random e.g. 1,3,7,9,12, you might have to create an array of different characters and work with that instead. Share this post Link to post Share on other sites
OpaQue 15 Report post Posted May 30, 2005 An Array of diffrent characters is not needed. Strings are stored as arrays internally. All you have to do is access the diffrent characters using the following syntax,$variable_name{index};index will be the character postion from 0. Try it out :-). Share this post Link to post Share on other sites
Mike 0 Report post Posted May 30, 2005 Like OpaQue said, you would use the substr() function. The only thing that I am not sure about in what OpaQue posted, is the $variable{n}. Unless that is another way to use the substr function, quickly of course. Basically the substr() function works like substr($variable, nlettertostartat); <?php$string = 'Testing';$substr = substr($string,3);echo $substr; // Result: 'ting'?> Actually, if you want to just get a certain character/characters, do what OpaQue said. $variable{n characetr [1,2,3,4,etc]}. Share this post Link to post Share on other sites
Tyssen 0 Report post Posted May 30, 2005 Sorry, I was thinking about a single function to extract a set of characters rather than a series concatenated together. Share this post Link to post Share on other sites
Corey 0 Report post Posted May 30, 2005 tnx everyone ..btw..do you know some simple script that can write the content of some folder..example- Downloads -1.rar -3.rar -5.jpg... Share this post Link to post Share on other sites
Tyssen 0 Report post Posted May 30, 2005 Here's a stripped down version of one I use on a site I'm developing for a friend: if ($handle = opendir($PathtoYourDirectory)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $file_array[] = $file; } } } closedir($handle); $result=array_unique($file_array); natcasesort($result); reset($file_array); foreach($result as $file){ echo $file."<br />"; } unset($file_array); Share this post Link to post Share on other sites