beginner question on multidimensional arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matthewburton@gmail.com

    beginner question on multidimensional arrays

    Hi everyone,

    I'm trying to write a program that will search a body of text and
    replace certain words (say, A, B, and C) with different words that I
    think are more appropriate (A1, B1 and C1). I have a feeling that I
    need to do this through a multidimensiona l array. Am I on the right
    track?

    thanks,
    matt

  • Chung Leong

    #2
    Re: beginner question on multidimensiona l arrays


    matthewburton@g mail.com wrote:[color=blue]
    > Hi everyone,
    >
    > I'm trying to write a program that will search a body of text and
    > replace certain words (say, A, B, and C) with different words that I
    > think are more appropriate (A1, B1 and C1). I have a feeling that I
    > need to do this through a multidimensiona l array. Am I on the right
    > track?
    >
    > thanks,
    > matt[/color]

    I don't really see a need for multidimensiona l arrays here. A single
    associative array would serve perfectly well. For example, the
    following is the simplest way to do what you described:

    $table = array("A" => "A1", "B" => "B1", "C" => "C1");
    $text = strtr($text, $table);

    For something requiring more sophisticated handling, you might want to
    use preg_replace(), passing twos arrays as parameters.

    Comment

    • crucialmoment

      #3
      Re: beginner question on multidimensiona l arrays

      Here's an example using preg_replace

      $pattern[0] = '/\/';
      $pattern[1] = '/\}/';
      $pattern[2] = '/\$/';
      $pattern[3] = "/\%/";
      $replacement[0] = '{';
      $replacement[1] = '}';
      $replacement[2] = '$';
      $replacement[3] = '%';
      $myvar = preg_replace($p attern, $replacement, $myvar);

      $pattern is the array of text or characters to search for.
      $replacement is what to replace with
      In this example, some characters are replaced with their html escape
      codes

      Comment

      • f3l_

        #4
        Re: beginner question on multidimensiona l arrays

        $txt=str_replac e(array('fuck', 'this','shit'), array('f***','t his','s***'),$t xt);

        Comment

        Working...