checking characters?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

checking characters?

Post 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?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
saumya
Forum Contributor
Posts: 193
Joined: Sun Jan 30, 2005 10:21 pm

Post by saumya »

thank you.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
Post Reply