FIXIT MENU:
home about us contact us

WHAT'S AVAILABLE:
free scripts advanced scripts online tools great books web related tutorials contributed tutorials news archive geek toys!

SUPPORT:
help forum live chat help

PHP: String Manipulation

This tutorial was developed to show how to find a specific character string in a larger string.
PHP offers some great character functions which allow manipulation and searching, including regex and str_replace.

What are You Trying to Do with the "nn" in Funny?

Well to answer your question exactly, I would want to know what you are planning to do exactly. If you know the input word "Funny" and just want to extract the characters "nn" it is very simple to do it using substri().

This code:

<?php
//set string equal to Funny
$str = 'Funny';

// extract the "nn" and print
echo substr($str, 2, 2);
?>

renders this:
nn

Finding the pattern "nn" in any word.

For this we turn to something called Regular Expressions, regex for short. Regex is a wonderful way of searching for specific patterns of characters. There are whole books on the subject, and it can be a very tough topic to initially break into, but once mastered, even at a basic level, you can get tremedous results. In this example, we test for the pattern - one or more numbers or letters / "nn" / one or more letters or numbers - in five words and return true (which is the value of 1 if it is present) or false (0) if it is not.

<?php
$str  = 'Funny';
$str1 = 'Monney';
$str2 = 'Bunny';
$str3 = 'Banana';
$str4 = 'nnotgoingtowork';


$pattern =
"/([a-zA-Z0-9])+nn([a-zA-Z0-9_-])+/";

// returns true
echo preg_match($pattern, $str);
print ' ' . $str . ' because has character(s)/nn/character(s)<br />';
echo preg_match($pattern, $str1);
print ' ' . $str1 . ' because has character(s)/nn/character(s)<br />';
echo preg_match($pattern, $str2);
print ' ' . $str2 . ' because has character(s)/nn/character(s)<br />';
echo preg_match($pattern, $str3);
print ' ' . $str3 . ' FALSE because has NO "nn"<br />';
echo preg_match($pattern, $str4);
print ' ' . $str4 . ' FALSE because has NO character before "nn"<br />';
?>

Produces:


1 Funny because has character(s)/nn/character(s)
1 Monney because has character(s)/nn/character(s)
1 Bunny because has character(s)/nn/character(s)
0 Banana FALSE because has NO "nn"
0 nnotgoingtowork FALSE because has NO character before "nn"

String Replacement Function

You can do a string replacement easily. The following code replaces ALL instances of the characters nn:

<?
$str = "...this if not Funny any more. It is nnot going to have any nn's anny more ...";

// returns the string without nn pattern ... substitute xx
echo str_replace("nn", "xx", $str);
?>

So we get xx's instead of nn's:

Lots More Help Out there:

For the HTMLfixIT tute on spliting strings, see: http://htmlfixit.com/cgi-tutes/tutorial_PHP_Splitting_a_String.php
If you want more on strings see: http://www.php.net/manual/en/ref.strings.php
If you want more on regex see: http://www.zend.com/manual/ref.regex.php

As always if you need personal help, come see us at htmlFixIt.com: http://htmlfixit.com

So if you are ready to learn more ...
Back to the Tutorial Index