Managing Strings
Given the use of PHP language, the management of strings is very important; there are several functions to manage strings, following is an explanation of the most widely used.
- strlen(string). Returns the number of characters in a string.
- split(separator,string). Divides a string into several, using a separation character.
- sprintf(string format, var1, var2...). Formats a text string as well as printf, but the result is returned as a string.
- substr(string, inicio, longitud).Returns a substring from another, beginning at start and of length.
- chop(string). Eliminates line feed and final spaces in a string.
- strpos(string1, string2). Search for string2 inside string1 indicating its position.
- str_replace(string1, string2, texto). Replaces string1 with string2 in the text.
<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
<title>PHP Example</title>
</head>
<body>
<?php
echo strlen("12345"),"<br>";
$words=split(" ","This is a test");
for($i=0;$words[$i];$i++)
echo $words[$i],"<br>";
$result=sprintf("8x5 = %d <br>",8*5);
echo $result,"<br>";
echo substr("Returns a substring from another",9,3),"<br><br>";
if (chop("String \n\n ") == "String")
echo "Equals<br><br>";
echo strpos("Search for string2 inside string1", "inside"),"<br><br>";
echo str_replace("green","red","A green fish like green grass."),"<br>";
?>
</body>
</html>