Friday, August 9, 2013

Reg-ex

Delimiters /@#`~%&'" try not to use quotes

$searchFor = preg_greg(%the%, $myArray);

foreach($searchFor as $searchResultArray)
{
  echo $searchResultArray, "<br>";
}

\d 0-9
\D excludes 0-9
\w a-z upper and lowercase, numbers, and underscores
\W excludes all \w
\s whitespace
\S excludes all \s
\b spaces between words (word boundaries)
\B excludes all \B


$searchFor = preg_greg(%\d{5}%, $myArray).......
//Search is for 5 digit numbers

$searchFor = preg_greg(%\d{1,5}%, $myArray).........
//Search is for 1-5 digit numbers

$searchFor = preg_greg(%\d{3,}%, $myArray).........
//Search is for 3 or greater digit numbers

$searchFor = preg_greg(%^<.*>(.*)<.*>$%, $myArray)........
//Search is for anything that would appears between two <tags>
//".*" denotes that nay numbers can occur any number of times
//The dollar sign says that at that point (the closing <tag>) the string will end.

$searchFor = preg_greg(%ass[essisture]%, $myArray).......
//Let's say I was looking for: assess, assist, assure

$searchFor = preg_greg(%^do%[^g|ug], $myArray)........
//Search is for don; omits dog or dug

More on this Later.....(Maybe)




Replace text 

<?php
$tweety = "I taught I taw a pooty tat";
$tweety = preg_replace("%taught%", "thought", $tweety);
$tweety = preg_replace("%taw%", "saw", $tweety);
$tweety = preg_replace("%pooty%", "kitty", $tweety);
$tweety = preg_replace("%tat%", "cat", $tweety);
echo $tweety;
?>//There's prolly a better way to do this

<?php
$tweety = "I taught I taw a pooty tat";
$tweety = string_replace("taught", "thought", $tweety);
$tweety = string_replace("taw", "saw", $tweety);
$tweety = string_replace("pooty", "kitty", $tweety);
$tweety = string_replace("tat", "cat", $tweety);
echo $tweety;
?>//There's prolly a better way to do this

        echo substr($tweety, 18, 5);
//This print form the 18th character for 5 characters (kitty)

No comments:

Post a Comment