Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a body of text that includes randomised numbers.

I make an array of the numbers, process them (look up relevant information to display based on each number) and then reinsert the changed array into the original text at the same locations I took the array from.

So far I am using preg_match_all to create the new array, then foreach for the process, then I insert these strings into a new array called $replacement.

So I have an array that I want to use to replace the strings in the original text. The problem is that I want the replacement calculated on the position value - regardless of what's in the array, string 1 matches string 1 by position in the array.

Difficult to explain. Here's an example.

Original text: Hello 1 Hello 2 Hello 3

original array {
    [0] 1
    [1] 2
    [2] 3
}
replacement array {
    [0] One
    [1] Two
    [2] Three
}

So when I do what I need to do, the original body text would look like:

Hello One Hello Two Hello Three

The catch is, I'd also want it to look like Hello One Hello Two Hello Three even if the original array was:

original array {
    [0] 1
    [1] 1
    [2] 1
}
replacement array {
    [0] One
    [1] Two
    [2] Three
}

How could I do that?

share|improve this question
in second case what is the criteria for replacing ? – Prasanth Bendra Jan 28 at 11:14
Location of each string. – porkchop sandwiches Jan 28 at 11:15
So basically you dont want your "original array" it all depends on second array (replacement array) and its key rite ? – Prasanth Bendra Jan 28 at 11:18
Pretty much! I only care about the positions of the original strings for the final replacement - other processing already done. – porkchop sandwiches Jan 28 at 11:24

2 Answers

up vote 1 down vote accepted

are you trying this :-

$original_array=array(1,2,3);
foreach($original_array as $key=>$value) {
  echo str_replace($key, 'Hello',$key);
}

output:- HelloHelloHello

share|improve this answer
Seems to work! Thanks! – porkchop sandwiches Jan 28 at 14:50

Is this one you looking for...???

$string="Hello 1 Hello 2 Hello 3";
    $rslt=preg_match_all('/(\d)/',$string,$matches);
    foreach ($matches[0] as $key=>$value){
        $digit= $key+1;
        $words=convertToWord($digit);
        $string= str_replace($value,$words,$string);
    }

echo $string;
function convertToWord($digit)
    {
        switch($digit)
        {
            case "0": return "zero";
            case "1": return "one";
            case "2": return "two";
            case "3": return "three";
            case "4": return "four";
            case "5": return "five";
            case "6": return "six";
            case "7": return "seven";
            case "8": return "eight";
            case "9": return "nine";
            case "": return "only";
        }
    }

OUTPUT:

Hello one Hello two Hello three

DEMO

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.