Best Online Test Resource for IT Certification                             
We provide Certification guides for MICROSOFT, CISCO, HP, ORACLE, COMPTIA, APPLE, CHECKOUT, EXAM EXPRESS, NORTEL , NOVELL.
Learn Languages
 


» Up
 

What is php?

Php Installations

Php Syntax

Php variables

Php Echo

Php Strings

Php Operations

Php Comments

Php Include File

Php Require

Php If-Statement

Php If-Else

Php Else-If

Php Switch

Php Forms

Php Functions

Php Arrays

Php While Loop

PHP For Loop

Php For Each

Php Do While

Php Post and Get

Php Magic Quotes

Php Files:

Php File

Php File Create

Php File Close

Php File Open

Php File Write

Php File Read

Php File Delete

Php File Append

Php File Turncate

Php File Upload

Php Strings:

Php strpos

Php str-replace

Php substr-replace

Php Capitalization

Php Explode

Php Implode

Php Advance:

Php Date

Php Sessions

Php Cookies

 

The Best It certification resource.

 

        

 

PHP SUB STR REPLACE

PHP substr_replace Function

The function substr_replace introduces some additional functionality to compliment str_replace. substr_replace is a more mathematically based replace function, which relies on starting points and lengths to replace parts of strings, as opposed to searching and replacing.

substr_replace's Four Parameters

There are three required parameters for the substr_replace function (original string, replacement string, starting point) and one that's optional (length).

  1. original string - This is your original string that will be operated on.
  2. replacement string - This string will be used to replace everything in the string from the starting point to the ending point (specified by length).
  3. starting point - This is the place in the original string that will be used to mark the replacement's beginning. A negative value specifies the number of characters from the end of the string.
  4. optional length - How many characters from the original string will be replaced. If no length is specified then the end of the string is used. If a value of 0 is used then no characters will be replaced and an insert is performed. A negative value specifies the number of characters from the end of the string.

substr_replace On Your Mark

This example of substr_replace shows what happens when you omit the length parameter at various starting points.

PHP Code:

//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 ";

Display:

Original String: ABC123 Hello Mr. Cow! DEF321
Starting Point 5: ABC12Five
Starting Point 12: ABC123 HelloTwelve
Starting Point 0: Zero
Starting Point -1: ABC123 Hello Mr. Cow! DEF32Negative 1

As you can see, when you don't specify the fourth parameter, length, everything after the starting point is replaced by the second parameter replacement string.

Note: The first replacement occurred at position 5, which in $original was the character 3. This 3 and everything onward was replaced with the replacement string. Remember that you start counting character to begin from zero. The $original string could be labeled as so:

  • Letter A - Position 0
  • Letter B - Position 1
  • Letter C - Position 2
  • Letter 1 - Position 3
  • Letter 2 - Position 4
  • Letter 3 - Position 5

substr_replace Specifying a Length

If you want to get any sort of precision out of this function you're going to have to get into the nitty gritty of specifying the exact length of characters you want replaced in your original string.

Imagine that you want to get rid of those ugly pseudo references (ABC123, DEF321) at the beginning and end of the string. Since both of those strings are a length of 6 and we know one is at the very beginning of the string and the other is at the very end of the string we should probably use a starting point of 0 for ABC123 and a value of -6 for DEF321. By having a replacement string of nothing "" we can do something similar to select and delete that we often do in a word processor.

PHP Code:

//string that needs to be customized
$original = "ABC123 Hello Mr. Cow! DEF321";

//remove ABC123 and store in $cleanedstr
$cleanedstr = substr_replace($original, "", 0, 6);
//remove DEF321 from $cleanedstr
$cleanedstr2 = substr_replace($cleanedstr, "", -6, 6);

//Echo each string
echo "Original String: $original <br />";
echo "Clean #1: $cleanedstr <br />";
echo "Clean #2: $cleanedstr2";

Display:

Original String: ABC123 Hello Mr. Cow! DEF321
Clean #1: Hello Mr. Cow! DEF321
Clean #2: Hello Mr. Cow!

Make sure that you play around with this function some on your own so you can get a feel for how the starting point and length parameters effect this function.

substr_replace Perform an Insert

By setting the length parameter to zero you can stop substr_replace from removing anything from the original string and just add to it. If we wanted to add a second and third person to our $original string we would want to do this insert operation. Note: instead of counting the characters we've used a couple other PHP functions to figure out the starting positions for us.

PHP Code:

//string that needs to be customized
$original = "Hello Mr. Cow!";

// Get the position of Mr. Cow
$cowpos = strpos($original, "Mr. Cow");

// Find where Mr. Cow ends by adding the length of Mr. Cow
$cowpos_end = $cowpos + strlen("Mr. Cow");

// Insert Mrs. Bear after Mr. Cow
$mrsbear = substr_replace($original, " and Mrs. Bear", $cowpos_end, 0);

// Insert Sensei Shark before Mr. Cow
$senseishark = substr_replace($mrsbear, "Sensei Shark, ", $cowpos, 0);


//Echo each string
echo "Original String: $original <br />";
echo "After Mrs. Bear: $mrsbear <br />";
echo "After Sensei Shark: $senseishark";

Display:

Original String: Hello Mr. Cow!
After Mrs. Bear: Hello Mr. Cow and Mrs. Bear!
After Sensei Shark: Hello Sensei Shark, Mr. Cow and Mrs. Bear!

We snuck a new function strlen into that example, but it isn't that complicated of a function, as it stands for "string length."

  • $cowpos_end = $cowpos + strlen("Mr. Cow");

The strlen function takes a string and then counts up how many characters are in it then returns that number. So by calculating the length of "Mr. Cow" and adding that to the position, we find out where the end point is!

 

 

 

 

TAGS Certification, Cisco, Microsoft, Oracle, HP, CompTIA+, free online resource ,MCSE, A+, Security+

 

 

 

 
© Copyright 2003-2004 Your Company. Web Site  Powered by  Beep Tech

Our Online resources for Windows 2003, Dot NET Windows 2000 , Back office, Cisco, Certifications, CompTIA+ , Security, IP Telephony, IT Support

 
 
 

 



 

Popular Study Exams

Popular Exams:

 
925-201b FN0-405 FN0-125 FN0-103 FN0-240 FN0-100 FN0-202 FD0-210 GD0-110 GD0-100 QQ0-300 HD0-200 QQ0-200 HD0-300
 HD0-400 HD0-100 QQ0-400 QQ0-100 HH0-110 HH0-120 GB0-320 GB0-280 GB0-180 GB0-360 4H0-028 4H0-020 4H0-004 4H0-533
4H0-200 4H0-712 4H0-100 4H0-110 4H0-002 4H0-435 II0-001 IL0-786 CISA SSCP CISSP BH0-001 630-008 630-005 630-007 630-006
JN0-340 JN0-320 JN0-521 JN0-310 JN0-303 JN0-561 JN0-140 JN0-350 JN0-311 JN0-341 JN0-130 JN0-530 LE0-406 190-755 190-832 190-
273 190-824 190-823 190-834 190-801 190-831 190-520 190-711 190-753 190-825 1T0-035 MD0-205 MD0-251 MD0-235 ML0-220
MK0-201 NS0-210 NS0-910 NS0-131 NS0-310 NS0-153 NS0-170 NS0-111 NS0-320 NS0-121 NS0-130 NS0-141 NS0-120 1T6-303 1T6-111
1T6-520 1T6-510 1T6-323 1T6-530 NO0-002 NQ0-231 50-683 50-662 50-876 50-695 50-886 50-676 50-895 50-888 50-663 50-635
50-865 50-704 UM0-300 UM0-100 UM0-401 UM0-200 CVE 1K0-001 RH202 RH302 3X0-102 3X0-203 3X0-104 3X0-204 3X0-201 3X0-202
3X0-103 3X0-101 A00-203 A00-212 A00-201 A00-211 A00-202 A00-204 A00-206 A00-205 SC0-502 SC0-411 SC0-501 SC0-402 SQ0-101
S10-101 S10-300 S10-100 S10-200 1T6-101 510-410 510-308 510-020 510-015 510-701 510-309 510-050 510-306 510-022 250-501
250-503 250-502 250-101 250-504 NR0-014 NR0-017 NR0-013 NR0-016 NR0-015 NR0-012 NR0-011 CCNT TT0-101 TIA-BDC TB0-106
 TB0-105 TB0-104 TB0-103 TB0-107 TU0-001 DP-023W DP-022W DP-002W BE-100W DP-023X DP-021W


 

3Com Adobe APC Apple BEA BICSI CheckPoint Cisco Citrix CIW CompTIA Computer Associates
CWNP Dell ECcouncil EMC Enterasys Exam Express EXIN Extreme Networks File Maker Fortinet Foundry
Fujitsu Guidance Software HID HITACHI Hewlett Packard Huawei Hyperion IBM IISFA Intel ISACA
ISC ISEB ISM Juniper Legato Lotus LPI McAfee McDATA Microsoft Mile2 Network Appliance Network General
 Nokia Nortel Novell OMG Oracle PMI Polycom Red Hat SAIR SAS Institute SCP SeeBeyond SNIA Sniffer
Sun Sybase Symantec Teradata TIA TIBCO Trusecure Veritas VMware

 

 

 


The material on this web site is not sponsored by, endorsed by or affiliated with Microsoft, Inc, CompTIA, or Cisco Systems, Inc. Microsoft, Inc.® ,Windows XP, Windows 2000, Windows 2000 Server, Windows 98, Windows NT, Visual Basic, Visual C++, Visual FoxPro, SQL server and Microsoft logos are trademarks or registered trademarks of Microsoft,  Inc. in the United States and certain other countries. A+, Server+, and Network+ are all registered trademarks of CompTIA. CCNA, CCNE, and the Cisco logo are all registered trademarks of Cisco Systems. All other trademarks are trademarks of their respective owners