Page 1 of 1

checking characters?

Posted: Thu Aug 18, 2005 5:38 am
by saumya
is it possible to check the first four characters of a string? Well to generalise this,Is it possible to check a buch of characters from a string?

Posted: Thu Aug 18, 2005 5:41 am
by raghavan20
you can use preg_match for searching a pattern is available in a string.
you can use preg_replace for replacing patterns with their appropriate patterns.
guess, you should have a look at
http://uk.php.net/manual/en/reference.p ... ifiers.php

Posted: Thu Aug 18, 2005 6:03 am
by saumya
thank you.

Posted: Thu Aug 18, 2005 8:10 am
by feyd
You may find regular expressions a large hurdle, like most have. That said, you could use a loop over the string to look for your pattern. Here's a start at it:

Code: Select all

$string = "hi hi I'm a string!";

for($i = 0, $j = strlen($string); $i < $j; $i++)
{
  if($string{$i} == 'h')
  {
    $string{$i} = 'H';
  }
}

echo $string;
outputs

Code: Select all

Hi Hi I'm a string!


granted, that example could be done with str_replace() quite easily. :)