Jump to content
xisto Community
Sign in to follow this  
Corey

2 Questions

Recommended Posts

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

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

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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.