PHP - Induvidual elements to Uppercase

Asked
Active3 hr before
Viewed126 times

6 Answers

elements
90%

Meta Stack Overflow ,I have got the to below and I a bit confused as it now capitalises each letter of the array rather than the letters selected in the 'if statement'. , Stack Overflow Public questions & answers , Stack Overflow help chat

You can use this code

function LetterChanges($str) {
   $arr = array();
   $strlen = strlen($str);
   for ($i = 0; $i <= $strlen; $i++) {
      $char = substr($str, $i, 1);
      ++$char;

      if ($char == "a" || $char == "e" || $char == "i" || $char == "o" || $char == "u") {
         $char = strtoupper($char);
      }
      if ($char == "aa") {
         $char = 'A'; //When we increase Z it becomes aa so we changed it to A 
      }
      $arr[] = $char;
   }
   //print_r($arr);
   echo implode("", $arr);
}

LetterChanges('hello*3');
load more v
88%

This integer value, is then converted to its corresponding character value using the chr() method. ,Split a comma delimited string into an array in PHP,How to remove the first character of string in PHP?,$str – The string to convert case of 

A for loop iteration is then performed over this array. Each character is validated, to verify if is a lower case alphabetic character. The ctype_lower() method is used to return a boolean value depending on which category the character belongs. This method returns TRUE, if the specified argument character is in lower case, else returns FALSE.

ctype_lower(ch)

In case, this method returns FALSE, the character is either a non-alphabetic character or an upper case character. If this scenario arises, the character is displayed unmodified. Else, the particular character is converted to upper case character, by subtracting ’32’ from its ASCII value. 

ord(ch) - 32
Output
Original String
Geeks ^
   for +Geeks
Uppercase String
GEEKS ^ FOR + GEEKS

Approach 2: Using strtoupper() method: The strtoupper() is an inbuilt method in PHP which carries out case-conversion. The alphabetics are all converted to upper case, and returned to the form of a string. The numbers and special symbols remain unmodified. The result has to be saved in a variable in order to preserve the changes. This method is faster and optimized in comparison to the previous approach.

strtoupper($string)
Output
Original String
Geeks ^
   for +Geeks
Uppercase String
GEEKS ^ FOR + GEEKS
load more v
72%

2 Converting to and from ASCII Values,4 Finding the Length of a PHP String,3.1 PHP printf Formatting Specifiers,1 Changing the Case of a PHP String

PHP provides a number of functions that enable changes to be made to the case of text contained in a string. These functions all take the string to be modified as an argument and return the modified string. It is important to note that these functions are nondestructive, that is to say they do not make any change to the original string, they simply return a completely new string containing the modification leaving the original string unaltered. The returned string can be assigned to a new variable or used directly in another function (such as a print or echo):

< ? php
$myString = "This is a test string.";

$newString = strtoupper($myString); // Assign converted string to a new variable

echo strtolower($myString); // Use retruned string in a eacho statement
?
>
load more v
65%

characters: Capitalize all characters — that is, uppercase.,Indicates whether input elements can by default have their values automatically completed by the browser. autocomplete attributes on form elements override it on <form>. Possible values:,words: Capitalize the first letter of each word.,Creates a hyperlink or annotation depending on the value, see the rel attribute for details.

<!-- Form which will send a GET request to the current URL -->
<form>
   <label>Name:
      <input name="submitted-name" autocomplete="name">
   </label>
   <button>Save</button>
</form>

<!-- Form which will send a POST request to the current URL -->
<form method="post">
   <label>Name:
      <input name="submitted-name" autocomplete="name">
   </label>
   <button>Save</button>
</form>

<!-- Form with fieldset, legend, and label -->
<form method="post">
   <fieldset>
      <legend>Title</legend>
      <label><input type="radio" name="radio"> Select me</label>
   </fieldset>
</form>
75%

As is generally understood, the position of an element is indicated as spanning from a start tag, possibly including some child content, and is terminated by an end tag.[1] This is the case for many, but not all, elements within an HTML document. The distinction is explicitly emphasised in HTML 4.01 Specification: ,This section lists some widely used obsolete elements, which means they are not used in valid code. They may not be supported in all user agents. ,The above section refers only to the detailed implementation of CSS rendering and has no relevance to HTML elements themselves. ,Continuing with this example, now that the section has been marked up as a target, it can be referred to from external sites with a link like: <a href="http://example.com#contents">see contents</a>;

<p>In the HTML syntax, most elements are written ...</p>
load more v
40%

Each individual trait that is imported into a class MUST be included one-per-line and each inclusion MUST have its own use import statement.,The use keyword used inside the classes to implement traits MUST be declared on the next line after the opening brace.,When the class has nothing after the use import statement, the class closing brace MUST be on the next line after the use import statement.,Otherwise, it MUST have a blank line after the use import statement.

This example encompasses some of the rules below as a quick overview:

< ? php

declare(strict_types = 1);

namespace Vendor\ Package;

use Vendor\ Package\ {
   ClassA as A, ClassB, ClassC as C
};
use Vendor\ Package\ SomeNamespace\ ClassD as D;

use
function Vendor\ Package\ {
   functionA,
   functionB,
   functionC
};

use
const Vendor\ Package\ {
   ConstantA,
   ConstantB,
   ConstantC
};

class Foo extends Bar implements FooInterface {
   public
   function sampleFunction(int $a, int $b = null): array {
      if ($a === $b) {
         bar();
      }
      elseif($a > $b) {
         $foo - > bar($arg1);
      } else {
         BazClass::bar($arg2, $arg3);
      }
   }

   final public static
   function bar() {
      // method body
   }
}
load more v

Other "elements-undefined" queries related to "PHP - Induvidual elements to Uppercase"