Im trying to spit out each letter of the alphabet from an array on a single line, A-Z.
This is what my code looks like so far:
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
while ($alphabet) {
echo $alphabet;
$alphabet;
}
Im kinda stuck at this part and not quite sure what else to write to make this work. Any suggestions?
Use range and array_walk:
function e($s) { echo $s; }
array_walk(range('A', 'Z'), 'e');
Working example: http://codepad.org/pedjOlY9
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
foreach($alphabet as $letter) {
echo $letter;
}
I am not sure why you need the array...
This is why we have the ASCII code.
You can do is like this:
for ($i = 65; $i <=90; $i++)
{
echo chr($i) . PHP_EOL;
}
chr() displays the character in the ASCII map - check it here: http://www.danshort.com/ASCIImap/. If you want to do lowercase - just use strtolower() or the numbers between 97-122 instead. PHP_EOL is a built in constant that outputs end of line. You can change it with ."" if you are doing HTML.
I think range is a little bit longer to be done, but it still works.
This might be help full for you
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$c = sizeof($alphabet);
for($i= 0; $i < $c ; $i++) {
echo $alphabet[$i];
}
and you can use count($alphabet) instead of sizeof() built-in function
Related
I have a small function to correct user entries if they do not capitalize their name in a form.
<pre>
function fixWords($x){
// define process
$x= strtolower($x);
$x= ucwords($x);
return $x;
}
</pre>
I am finding that double names and hyphenated names are not getting capitals. What is the best way to split out doubles and hyphenated names to force capitals on those also. Thanks.
You can use ucfirst() and preg_replace_callback(), i.e.:
function fixWords($name)
{
return preg_replace_callback('/\b\w/i', function($matches) {
return ucfirst(strtolower($matches[0]));
}, $name);
}
print fixWords("some name");
# Some Name
print fixWords("some-name");
# Some-Name
PHP Demo
Add this in the mix:
$xArr = explode('-', $x);
$i = 0;
while($i < count($xArr)) {
$xArr[$i] = ucfirst($xArr[$i]);
$i++;
}
$x = join('-', $xArr);
http://php.net/manual/en/function.ucfirst.php
I know what i have implemented here is wrong i want it to do it correctly that is why asking help here.Don't know whether this is possible or not.
<?php
$test1="hello";
$test2="how";
$test3="are";
for($i=1;$i<=3;$i++)
{
echo $test.$i;
}
?>
When i run this i should get hello how are .i know string concatenation same thing i want to do it for variable also.Is this Possible, if possible by this i can easily access all those variable. Any help?
Try with following syntax:
echo ${'test'.$i};
I guess what you're looking for is the "array". You can use arrays in PHP like this:
$test = array('hello', 'how', 'are');
$len = count($test);
for($i=0; $i<$len; $i++) {
echo $test[$i];
}
Try this:
for($i = 1; $i < 4; $i++){
echo $test{$i};
}
I am trying to form an acronym from a given text. The Idea here is that the first Letter in $text ($text[0]) will be taken and placed inside the array $storage using array_push(). Now, if there is a space inside the array, the letter of the next index should be a part of the Acronym. I am currently not getting an ouput, what am I missing?
public function Acronym($text)
{
$text = str_split($text);
$count = strlen($text);
$storage = array();
for($i=0; $i<$count; $i++)
{
array_push($storage, $text[0]);
if($text[$i]==' ')
{
array_push($storage, $text[$i+1]);
}
foreach($storage as $clean)
{
echo $clean;
}
}
}
Your algorithm suffers from a few fatal flaws:
You're calling strlen() on an array, when you should be calling count():
$text = str_split($text);
$count = count($text);
However, you can index strings as arrays, so you don't need str_split() in this scenario, and you can keep $count = strlen( $text); by removing the call to str_split().
This should only happen once, so it should be outside the loop (This implies starting $i at 1):
array_push($storage, $text[0]);
Your foreach loop that prints the $storage array should be outside of the loop that is creating the acronym.
You can save the overhead of calling a function by using the shorthand array_push() notation. You should use array_push() when adding more than one element to an array. Otherwise, this will suffice:
$storage[] = $text[0];
You need to return something from your function, otherwise you won't be able to access anything outside of it.
Put that all together, and you get this:
public function Acronym($text)
{
$count = strlen( $text);
$storage[] = $text[0];
for( $i = 1; $i < $count; $i++)
{
if( $text[$i] == ' ')
{
$storage[] = $text[$i+1]);
$i++; // Can increment $i here because we know the next character isn't a space
}
}
foreach($storage as $clean)
{
echo $clean;
}
return $storage;
}
That being said, there are far better implementations for forming an acronym giving a string input. Here is one that I can think of:
public function Acronym( $text)
{
$acronym = array();
foreach( explode( ' ', $text) as $word)
{
$word = trim( $word);
$acronym[] = strtoupper( $word[0]);
}
return implode( '', $acronym);
}
Note that both functions will fail for inputs like Hello World. I am leaving it up to the OP to make these modifications (if necessary).
str_split turns the string into an array.
str_length brings the length of a string which you have overwritten with an array already. you need count()
You overwrite your first variable $text
$count = strlen($text);
In this line $text is an array, because you changed it in the first line of your method.
Try inverting the two first lines:
$count = strlen($text);
$text = str_split($text);
Note
This will solve your secondary problem, and enable your algorithm to run without errors. It doesn't fix your algorithm, but at least you will be able to debug it now.
you are running your loop on $count which is getting its value from str_len its an array because of return on $text = str_split($text);
So you have overwritten your $text variable you can fix it by changing order get length first then split.
PHP has the function range() which can be used to loop through letters a-z. Is there something similar or a similar function that can let me loop through hex values 0-9 + a-f?
array_merge( range(0,9), range('a','f') )
I think you mean ASCII value equivalents, you can do:
$merged_result=array_merge(range('0','9'),range('a','f'));
The numbers don't actually change, just how you are outputting them. So something like:
for (range(0,15) as $value) {
$value = base_convert($value,10,16);
}
No need to do that loop from 0-15. dechex($yournumber).
0x0 = 0
0xF = 15
for($i = 0; $i < 15; $i++) {
print dechex($i);
}
This is my function
http://www.ideone.com/slpPe
If i don't use function
preg_match_all("{http://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}[^\s\"\\\'\<&]*}ix", $html, $marches);
$download = $marches[0];
//var_dump();
$i = 0;
for ($i; $i <= count($download); $i++) {
echo "download|";
$links = $download[$i];
if (!eregi("avaxhome.ws|pixhost.info", $links)) {
echo $links . "<br/>";
}
}
and return some link
download|
http://www.filesonic.com/file/24394419/1851684883.rar
http://depositfiles.com/files/g6wawevha
Please help me fix it
Two problems:
for ($i; $i <= count($download); $i++) {
// You are re-initializing $return in the loop..overwriting it.
$return = "download|";
Just move the initilization outside the loop as:
$return = "download|";
for ($i; $i <= count($download); $i++) {
Also you should not use <= in:
for ($i; $i <=count($download)
it should be just <. This is because in an array of size N, N is not a valid index. The valid indices are 0 to N-1.
Also ereg family of function are deprecated as of 5.3.0 so its better you stop using them. You can use preg_match instead.
if (!eregi("avaxhome.ws|pixhost.info", $links))
can be written using preg_match as:
if (!preg_match("/avaxhome\.ws|pixhost\.info/i", $links))
Also note that the . in the regex needs to be escaped to treat it as a literal period.
Working link
Try using php's preg_match() instead of ereg()
http://www.php.net/manual/en/function.preg-match.php