4

if i have two arrays i.e

$text = 'i am passed :)';
$fn = array(
':)',
':-)',
';;)'
)

$rep = array(
'smily1',
'smily2',
'smily3'
            );

$output = str_replace($fn, $rep, $text);

echo $output;

i want to make a class for this to use in future where i will want... how can i make a class for it...

and also how can i create a function for this...

4
  • 1
    I don't really see a technical question here. Why not use the method you've already got? It would be simple to create a class to wrap around this functionality. Commented Jan 6, 2010 at 20:00
  • 3
    what do you want this class to do? You're looking to create a "class SmileyReplacer"? What's wrong with what you have, and why should it be a class? Commented Jan 6, 2010 at 20:01
  • You might also take a look at this question: stackoverflow.com/questions/65091/… Commented Jan 6, 2010 at 20:13
  • Heh. I didn't remember that was my question. Not fishing for votes, honest :) Commented Jan 6, 2010 at 20:13

3 Answers 3

5

Basically by wrapping your function in a class. If you're looking for more advanced functionality then that, you'll have to specify.

<?php

class SmileyFilter {
  private $_keys;
  private $_values;

  function add($key, $value) {
     $this->_keys[] = $key;
     $this->_values[] = $value;
  }

  function add_all($pairs) {
    foreach ($pairs as $key => $value)
      $this->add($key, $value);
  }

  function replace($text) {
    return str_replace($this->_keys, $this->_values, $text);
  }
}

// usage

$s = new SmileyFilter();

$s->add(':)', 'smily1');
$s->add(':-)', 'smily2');
$s->add(';;)', 'smily3');

/* OR

$smileys = array(
  ':)'  => 'smily1',
  ':-)' => 'smily2',
  ';;)' => 'smily3');

$s->add_all($smileys);

*/


$s->replace('i am passed :)'); // "i am passed smily1"
?>
Sign up to request clarification or add additional context in comments.

Comments

3
class SmileyReplacer
{
    protected static $_map = array(
        ':)'  => 'smiley1',
        ':-)' => 'smiley2',
        ';;)' => 'smiley3'
    );

    public static function replace($string)
    {
        return str_replace(array_keys(self::$_map), self::$_map, $string);
    }
}

// Usage
echo SmileyReplacer::replace('I am happy :)'); // I am happy smiley1

I see no reason why this should be instantiated, so an all static class is fine. There is no real state in it. You could add a static method addMap(array $map) which you could pass an associate array in case you want to feed the map from outside.

If you are concerned about the calls to array_keys each time you run replace, do benchmark. I highly doubt you can come up with enough smileys so it would really have an impact on performance.

Comments

2

Smiley

class Smiley {
    private $name;
    private $data;

    public function getName() {
    return $this->name;
    }

    public function setName($name) {
    $this->name = $name;
    return $this;
    }

    public function getData() {
    return $this->data;
    }

    public function setData($data) {
    $this->data = $data;
    return $this;
    }
    function __construct($name = null, $data = null) {
    $this->name = $name;
    $this->data = $data;
    }

}

SmileyMapper

class SmileyMapper {
    private $smilies = array();

    public function addSmiley(Smiley $smiley) {
    $this->smilies[] = $smiley;
    return $this;
    }
    public function replaceSmileys($str) {
    return str_replace(
        array_map(
            create_function(
            'Smiley $item',
            'return $item->getData();'
            ),
            $this->smilies),
        array_map(
            create_function(
            'Smiley $item',
            'return $item->getName();'
            ),
            $this->smilies),
        $str
    );
    }
}

Example

$text = 'i am passed :)';
$fn = array(
    ':)',
    ':-)',
    ';;)'
);

$rep = array(
    'smily1',
    'smily2',
    'smily3'
);

$sm = new SmileyMapper();
foreach ($fn as $k => $v) {
    $sm->addSmiley(new Smiley($rep[$k],$v));
}
echo $sm->replaceSmileys($text);

4 Comments

Now that is seriously overengineered, but fun :)
I thought it would be appropriate on a subject like smileys. :P
Smiley should implement an interface. Just to make sure. And why not use an SplObjectStorage for SmileyMaper::smileys :) err smiley1
I also think it could be necessary to implement a templating engine to account for different view requirements. Wouldn't you agree?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.