'string'에 해당되는 글 1건
- 2008.11.04 PHP - String Sample Code
PHP - String Position - strpos
$numberedString = "1234567890"; // 10 numbers from 1 to 0
$fivePos = strpos($numberedString, "5");
echo "The position of 5 in our string was $fivePos";
PHP str_replace Function
//string that needs to be customized $rawstring = "Welcome Birmingham parents. Your replaceme is a pleasure to have!"; //male string $malestr = str_replace("replaceme", "son", $rawstring); //female string $femalestr = str_replace("replaceme", "daughter", $rawstring); echo "Son: ". $malestr . "<br />"; echo "Daughter: ". $femalestr;
PHP substr_replace Function
//string that needs to be customized $original = "ABC123 Hello Mr. Cow! DEF321"; //starting point 5 $sp5 = substr_replace($original, "Five", 5); //starting point 12 $sp12 = substr_replace($original, "Twelve", 12); //starting point 0 $sp0 = substr_replace($original, "Zero", 0); //starting point -1 $spneg1 = substr_replace($original, "Negative 1", -1); //Echo each string echo "Original String: $original <br />"; echo "Starting Point 5: $sp5 <br />"; echo "Starting Point 12: $sp12 <br />"; echo "Starting Point 0: $sp0 <br />"; echo "Starting Point -1: $spneg1 ";
PHP - String Capitalization Functions
$originalString = "String Capitalization 1234";
$upperCase = strtoupper($originalString);
echo "Old string - $originalString <br />";
echo "New String - $upperCase";
PHP - String Explode
$rawPhoneNumber = "800-555-5555"; $phoneChunks = explode("-", $rawPhoneNumber); echo "Raw Phone Number = $rawPhoneNumber <br />"; echo "First chunk = $phoneChunks[0]<br />"; echo "Second chunk = $phoneChunks[1]<br />"; echo "Third Chunk chunk = $phoneChunks[2]";
PHP - Array implode
$pieces = array("Hello", "World,", "I", "am", "Here!"); $gluedTogetherSpaces = implode(" ", $pieces); $gluedTogetherDashes = implode("-", $pieces); for($i = 0; $i < count($pieces); $i++){ echo "Piece #$i = $pieces[$i] <br />"; } echo "Glued with Spaces = $gluedTogetherSpaces <br />"; echo "Glued with Dashes = $gluedTogetherDashes";
Piece #0 = Hello
Piece #1 = World,
Piece #2 = I
Piece #3 = am
Piece #4 = Here!
Glued with Spaces = Hello World, I am Here!
Glued with Dashes = Hello-World,-I-am-Here!