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:
...this if not Fuxxy any more. It is xxot going to have any xx's axxy more ...

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



This site is totally free to use, you have absolutely no moral or legal obligations to help us continue.
There are however, some costs involved in running the site.

<random humor>
Plus Franki needs to get his girl out on a date soon.
</random humor>

So if this site helped you find your way, perhaps you could consider contributing to our costs. Whatever amount you feel this site was worth to you would be just wonderful.
Use PayPal if you do decide to share and help us with the costs and in appreciation for our time and attention, or alternatively buy a book from our Bookstore..


  Time  in  Don's  part  of the world is:   March 12, 2010, 1:37 am
  Time in Franki's part of the world is:   March 12, 2010, 2:37 pm
  Don't worry neither one sleeps very long!



privacy policy :: support us :: home :: live chat help
contact us :: forum ::tutorials :: bookstore :: Site Map



      Valid XHTML 1.0! Valid CSS!       Community ENTerprise Operating System       powered by Apache Server
Pic 3 Pic 3

SEARCH:
USEFUL LINKS:

CIGHTML Firefox Thunderbird ClamWin WordPress SpyBot S&D TheGIMP Apache for Windows Registry Cleaners More cool stuff:








SEARCH:
USEFUL LINKS:

CIGHTML Firefox Thunderbird ClamWin WordPress SpyBot S&D TheGIMP Apache for Windows Registry Cleaners More cool stuff: