Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 10-12-2007, 02:57 PM   #1 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
Search $stringreplaced variable for I's and e's

I currently have code that searches a string for occurrences of different numbers stored inside an array. Upon a digit being found, the number in words is stored in the $stringreplaced variable.

The code then searches the $string variable for occurrences of I's and E's, and if any are founds, displays an appropriate message "x was found". How do I search the $stringreplaced variable instead for I's and E's, or in other words, the text display on line 3:

Orig string: 1234567ie89
I was foundE was found
onetwothreefourfivesixsevenieeightnine
- The variable to display this, search all the text in bold on this line for i's and e's.

Code:
<?php
$find = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
$replace = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');

$string = '1234567ie89';
echo 'Orig string: ' .$string . '<br />';


$sarray = str_split($string, 1);
for($i = 0; $i < count($sarray); $i++)
{
    if(strtoupper($sarray[$i]) ==  'E')
        echo 'E was found';
    elseif(strtoupper($sarray[$i]) ==  'I')
        echo 'I was found';
    
    $found = array_search($sarray[$i], $find);
    if($found !== false)
        $sarray[$i] = $replace[$found];
}

$stringreplaced = implode('', $sarray);
echo '<br />' . $stringreplaced; 
?>
Love.Oasis is offline   Reply With Quote
Old 12-30-2007, 07:47 PM   #2 (permalink)
xrowNow
Recruit
 
Join Date: Mar 2003
Posts: 2
xrowNow is on a distinguished road
Quote:
Originally Posted by Love.Oasis View Post
I currently have code that searches a string for occurrences of different numbers stored inside an array. Upon a digit being found, the number in words is stored in the $stringreplaced variable.

The code then searches the $string variable for occurrences of I's and E's, and if any are founds, displays an appropriate message "x was found". How do I search the $stringreplaced variable instead for I's and E's, or in other words, the text display on line 3:

Orig string: 1234567ie89
I was foundE was found
onetwothreefourfivesixsevenieeightnine
- The variable to display this, search all the text in bold on this line for i's and e's.

Code:
<?php
$find = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
$replace = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');

$string = '1234567ie89';
echo 'Orig string: ' .$string . '<br />';


$sarray = str_split($string, 1);
for($i = 0; $i < count($sarray); $i++)
{
    if(strtoupper($sarray[$i]) ==  'E')
        echo 'E was found';
    elseif(strtoupper($sarray[$i]) ==  'I')
        echo 'I was found';
    
    $found = array_search($sarray[$i], $find);
    if($found !== false)
        $sarray[$i] = $replace[$found];
}

$stringreplaced = implode('', $sarray);
echo '<br />' . $stringreplaced; 
?>
I am curious if this is still valid or if you are still looking for an answer to this question as it has been 2+months since your posting. However I will answer this question in an attempt to help not only you but people who might look for this answer in the future.

As I understand it, you are looking to replace 123 with onetwothree and count the occurrences of the letters I and E.

Very well, I would break these up into two different functions so each function may perform it's own action. The first we will call findChar and it will take two variables, the first would be the variable you are searching for and the second will be the string to search.
PHP Code:
function findChar($char$string) {
  return 
preg_match(/$char/i$string);

This function will allow us to reuse it for what ever character you would like to find.
PHP Code:
$string "mcndhskei1372idj";
if(
$i findChar('i'$string)) { 
  echo 
"I was found $i time(s)<br />\n";

Now you are looking to replace 123 with onetwothree.
PHP Code:
function digit2word($string) {
  
$words = array('zero','one','two','three','four','five','six','seven','eight','nine');
  
$replaced '';
  for(
$i=0$i<strlen($string); $i++) {
    if(
is_numeric($string[$i])) {
      
$replaced .= $words[$string[$i]];
    } else {
      
$replaced .= $string[$i];
    }
  }
  return 
$replaced;
}

$string '867-5309';

echo 
digit2word($string); 
Using these two functions you could find all the e's or i's in either the string before the replacement or the string after it being replaced with words for each of the digits.

I hope this helps,

xrowNow
xrowNow is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:07 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting