|
|
|
|
![]() ![]() |
May 29 2005, 05:39 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 218 Joined: 13-January 05 Member No.: 3,267 |
i have 2 questions about PHP...please don't answer like "use google"...or something...because i tried and didn't find anything..so
1. 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 $var tnx;) |
|
|
|
May 29 2005, 06:05 PM
Post
#2
|
|
|
Administrator ![]() Group: Admin Posts: 1,459 Joined: 11-June 04 From: Somewhere in Time & Space. Member No.: 1 |
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. |
|
|
|
May 30 2005, 12:08 AM
Post
#3
|
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 1,161 Joined: 9-May 05 From: Brisbane, QLD Member No.: 6,818 |
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. |
|
|
|
May 30 2005, 12:16 AM
Post
#4
|
|
|
Administrator ![]() Group: Admin Posts: 1,459 Joined: 11-June 04 From: Somewhere in Time & Space. Member No.: 1 |
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 :-). |
|
|
|
May 30 2005, 12:43 AM
Post
#5
|
|
|
Owner of Sub-Zero ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 159 Joined: 17-November 04 Member No.: 2,325 |
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);
CODE <?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]}. |
|
|
|
May 30 2005, 01:36 AM
Post
#6
|
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 1,161 Joined: 9-May 05 From: Brisbane, QLD Member No.: 6,818 |
Sorry, I was thinking about a single function to extract a set of characters rather than a series concatenated together.
|
|
|
|
May 30 2005, 08:01 AM
Post
#7
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 218 Joined: 13-January 05 Member No.: 3,267 |
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 ... |
|
|
|
May 30 2005, 12:03 PM
Post
#8
|
|
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 1,161 Joined: 9-May 05 From: Brisbane, QLD Member No.: 6,818 |
Here's a stripped down version of one I use on a site I'm developing for a friend:
CODE 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); |
|
|
|
![]() ![]() |
Similar Topics