basic regular expressions in php

Post Reply
darknkreepy3#
Site Admin
Posts: 254
Joined: Tue Oct 27, 2009 9:33 pm

basic regular expressions in php

Post by darknkreepy3# »

this is a simple program to find the first letter or series of numbers in a string and replace it with a font size tag with the letter or number(s) inside of it:

A-Z
is all the capital letters from A-Z

[A-Z]
just groups the range like parenthesis in algebra to simplify viewing (6*9)(8*3)
[A-Z]|[0-9] either or each range, same as [A-Z0-9]

([A-Z]|[0-9])
Matches either or, same as (A-Z|0-9)


/([A-Z])/
means this is a regular expression NOT a string variable

Code: Select all

<?php
$str1="this Is a Test and it should Work Fine";
$fixedStr=preg_replace("/([A-Z])/","<font size=\"5\">$1</font>",$str1);
echo "<h4>$str1</h4>";
echo "<h5>$fixedStr</h5>";
//-----
echo "<hr />";
//-----
$str2="11th Hour or Maybe 1986 or Possibly 1st 2nd Try";
$fixedStr2=preg_replace("/([A-Z]|[0-9])/","<font size=\"5\">$1</font>",$str2);
echo "<h4>$str2</h4>";
echo "<h5>$fixedStr2</h5>";
?>
Post Reply