将字符串中的单词替换为数组中的对象

时间:2016-10-11 13:05:31

标签: php

目前我从字符串中获取单词,用来自对象数组的html代码替换每个元素。

有没有更简单的方法来写这个?我目前有20个元素,看起来很长。

class Roles
{
    public $name;
    public $html;
}

$Administrator = new Roles();
$Administrator->name = "Administrator";
$Administrator->html = "<a class='administator'>Administrator</a>";

$Mod_Helpers = new Roles();
$Mod_Helpers->name = "Mod-Helpers";
$Mod_Helpers->html = " <a class='modhelpers'>Mod-Helpers</a>";

$Mod_Coders = new Roles();
$Mod_Coders->name = "Mod-Coders";
$Mod_Coders->html = " <a class='modcoders'>Mod-Coders</a>";

$Mod_Artists = new Roles();
$Mod_Artists->name = "Mod-Artists";
$Mod_Artists->html = " <a class='modartists'>Mod-Artists</a>";

$ModSquad = new Roles();
$ModSquad->name = "ModSquad";
$ModSquad->html = " <a class='modsquad'>ModSquad</a>";

$ModSquad = new Roles();
$ModSquad->name = "ModSquad";
$ModSquad->html = " <a class='modsquad'>ModSquad</a>";

$RoleList = array($Administrator, $Mod_Helpers, $Mod_Coders, $Mod_Artists, $ModSquad);

foreach ($RoleList as $role) {
    $roles = str_replace($role->name, $role->html, $roles);
}

0 个答案:

没有答案