|
|
|
String Fundamentals
===================
Strings are the basic Data Types in PHP. In this tutorial we discuss many text
manipulating techniques.
Before we move further have a look on echo function.
Comments
========
//This is a comment.
/* C style comments are also supported */
echo()
======
echo outputs one or more strings.
echo arg1,agr2;
echo "Hallo Word";
$girl="Ritu";
echo "Girl is $fg";
echo "escaping characters is done \"Like this\"."
echo() also has a shortcut syntax, where you can immediately follow the opening tag
with an equals sign.
I have <?=$model?> Car.
Finding Parts of String
substr() function :
substr($str,$initial_pos,$str_len)
$str="net-rpx infoways";
$ch=substr($str,1,3) ; //net
$end_char=substr($str,-1); //s
$end_word=substr($str,-8) ; //infoways In string counting always begin at zero not
one.
strpos Gives the position of the first character in a string that matches the
character you specify
<?php
$email="pradeepmamgain@rediffmail.com";
$user_name=substr($email,0,strpos($email,"@"));
echo $user_name; //pradeepmamgain
?>
If you want to read fixed length records use PHP [unpack()] function Its faster and
save CPU time. unpack function put strings in an associative array.
<html>
<body>
<?php
$str="Visual Basic is g8";
$unpstr=unpack("A6name1/x7/A2name2",$str);
echo $unpstr["name1"]; //Visual
echo "<br>";echo $unpstr["name2"]; //is
?>
</body>
</html>
A6name1 : A6 specify that the function is to read five characters, A is for space
padded strings.
Few more ...
a - nul padded string
A-Space padded stringsc - signed char
C-unsigned chars -
s-short
S-short
i-integer
I-integer
x -nul byte skip ahead characters
X-Back one bite
Trimming Blanks from the string
===============================
use chop() or rtrim() function to remove whitespaces.
<?php
$str="Rajdhani Express ";
// chop() remove whitespaces from right of
$str=chop($str) //analogous to rtrim($str)
?>
the string. use ltrim() function to remove white spaces from the left of the string.
Converting case
===============
<html>
<body>
<?php
$str="visual basic is g8 language";
$str=strtoupper($str);
echo $str; // VISUAL BASIC IS G8 LANGUAGE
echo "<br>";
$str=strtolower($str);
echo $str; // visual basic is g8 language
echo "<br>";
$str=ucfirst($str);
echo $str; // Visual basic is g8 language
echo "<br>";
$str=ucwords($str);
echo $str; // Visual Basic Is G8 Language
?>
</body>
</html>
before we move further lets have a look on
Regular Expression functions
============================
In these functions expressions should be closed in delimeters, a forward (/) slash
for example.Any character can be used for delimeter as far it is not alphanumeric or
backslash(\). If the delimeter has to be used in the expression itself, it need to
be escaped by backslash.Examples/<\/\e+>/ {^s+(\s+)?$}[b] [preg_match] [/b]Performs
a regular expression match. Returns true if a match found.
<html>
<body>
<?php
$str="IBM is the best machine.";
if (preg_match("/IBM/i",$str)){ print "A match is found.";
}else{ print "Sorry ... ";}
?>
</body>
</html>
i in "/IBM/i" indicates case-insensative search.
Find a Word
===========
<?php
$str="netrpx infoways the best tutorial provider.";
if (preg_match("/\binfoways\b/i",$str)){
print "A match is found.";
}else{ print "Sorry ... ";}
?>
the \b in the pattern indicates a word boundary so only distinct word "infoways" is
searched. Strings can be replaced using preg_replace.[b] [preg_spilit()][/b]array
preg_split( string pattern,string subject],int limit[, int flags]])If the limit is
specified , then only substrings upto limits are returned if limit is -1 then it
means no limit.Flag - PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE
<html>
<body>
<?php
$str="netrpxinfoways";
$Chars=preg_split("//",$str,-1, PREG_SPLIT_NO_EMPTY);
print_r($chars); // PRINTS ARRAY OF CHARS
?>
</body>
</html>
following will split netrpx:infoways into an array of netrpx and infoways
<?php
$str="netrpx:infoways";
$chars=preg_split("/:/",$str,-1,PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>
Reversing Words and Characters
==============================
In this tutorial section array_reverse function will be used which reverse an array.
[Implode]string implode (string glue,array pieces)Reverse string by word
<?php
$str="Howdy Suresh, whats up ?";
$chars=preg_split("/\s+/",$str,-1,PREG_SPLIT_NO_EMPTY);
$chars=array_reverse($chars);
$chars=implode(" ",$chars);
echo $chars;
?>
To reverse all chars in a string use strrev() function.
<?php
$str="Howdy Suresh, whats up ?";
$str=strrev($str);echo $str;
?>
Concating two strings
=====================
<?
$str="This is string";
$str.="and this is addition to it";
?>
|
|
| mysql_escape_string Categories : PHP, MySQL, Databases, Strings | | | Allows you to parse a deliniated string and put the individual fields in a SELECT option in a form Categories : HTML, PHP, Strings | | | function textwrap will wrap text to any desired width using <BR>\n as the default line break.
Default wrap width is 80 columns.
Categories : Strings, HTML and PHP, PHP | | | Adding dashes to credit card numbers Categories : Strings, Credit Cards, PHP | | | Customizable encoding and decoding strings with security. Categories : PHP, Strings, HTML and PHP | | | I need a trim function/regexp that will trim all " " from the ends of a string. Categories : Regexps, PHP, Strings | | | Printer friendly pages from anywhere on a website. Categories : PHP, Strings, Content Management | | | Look for the *position* of the first occurence of string2
in string1, beginning at position start.
Categories : Complete Programs, PHP, Strings | | | Mimic ASP's GetString function with PHP Categories : PHP, Databases, MySQL, Strings | | | How to control the number of decimal places when outputting numbers. Categories : PHP, Strings, Variables | | | The Porter Word Stemming Algorithm in PHP
Reduces words to their base stem for search engines and indexing Categories : Algorithms, PHP, Strings | | | PHP5 URL Object Categories : PHP, PHP Classes, URLs, Strings | | | Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP | | | columned txt file to array()? Categories : Arrays, Strings, Regexps, PHP | | | Pull deliniated text strings into a "SELECT" statement in a form. Categories : HTML and PHP, PHP, Strings | |
| | | | Hasan Robinson wrote :848
This is a great example. Kudos to the author.
I thought I might add this example to compliment the username substr example.
------------------------------------------------------------EXTRACTING A USERNAME FROM EMAIL ADDRESS
------------------------------------------------------------
$email="info@unknowndesigns.com";
$username=substr($email,0,strpos($email,"@"));
// this will print <b>info</b>
$domain=substr($email,strpos($email,"@")+1);
// this will print <b>unknowndesigns.com</b>
| |
|
|
|