PDA

View Full Version : String Search


TH.Sharplet
10-11-2007, 10:42 PM
Can a string be searched for specific characters? - Each time a 6 is found, can "Six" be displayed on the screen, and each time a 5 is found, can "Five" be displayed on the screen.

Fumigator
10-11-2007, 11:04 PM
str_replace() (http://us.php.net/manual/en/function.str-replace.php)

TH.Sharplet
10-11-2007, 11:15 PM
Using the following code, what needs to be changed, in order to do the following:

Display "Six" on the screen each time a 6 is found within the string, and display "Five" on the screen if 5 is found within the string.

<?php
$string = "12166784";
$stringreplaced = str_replace($find, $replace, $string);
echo 'Origional String'.$string.'<br>';
echo $stringreplaced;
?>

Fumigator
10-11-2007, 11:39 PM
I would use two arrays, one to list the numbers you want to change, and one to list the text you want each number to change to.


$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
$numberText = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'zero');
$myString = str_replace($numbers, $numberText, $myString);

TH.Sharplet
10-11-2007, 11:46 PM
I would use two arrays, one to list the numbers you want to change, and one to list the text you want each number to change to.


$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
$numberText = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'zero');
$myString = str_replace($numbers, $numberText, $myString);


That doesn't search a string for I's or E's?

Fumigator
10-12-2007, 12:37 AM
You've mentioned nothing about I's or E's to this point.

TH.Sharplet
10-12-2007, 12:46 AM
Oops!Sorry, I mean: "How do I search the following string for characters 'e' and 'i'?"

If an 'e' is found, can "Found E" being displayed on the screen.
If an 'i' is found, can "Found I" being displayed on the screen.

$string = "nineonethreefoursixseveneight";

Fumigator
10-12-2007, 02:40 AM
You probably want to use strpos() (http://us2.php.net/manual/en/function.strpos.php) for that.

Is this your homework?

TH.Sharplet
10-12-2007, 11:31 AM
You probably want to use strpos() for that.How?
Is this your homework?
No, this is not homework. I just need to search a string for occurrences of two characters, these being "E's" and "I's".