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
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:
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.
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:
July 30, 2010, 3:16 am
Time in Franki's part of the world is:
July 30, 2010, 4:16 pm
Don't worry neither one sleeps very long!
privacy policy ::
support us
:: home
:: live chat help
contact us
:: forum
::tutorials
:: bookstore
:: Site Map
