Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AwadGorg's avatar

Cleaing up some php code

hello I have this messy code and it gets messier every time I add to it and I want to make it cleaner but couldn't do it myself can you please help me with this and thanks

// those are the var used to replace words from an article to bold the specific word if found at the string
@php    
$Tags = @explode(',',$game_info->g_tags);
$g_tag = current($Tags);
$Tags = @explode(',',$game_info->g_tags);
$s_gamename = "<span class='text-light'>$game_info->g_name</span>";
$b_3d = "<b class='text-light'>3D</b>";
$b_2d = "<b class='text-light'>2D</b>";
$b_crossplatform = "<b class='text-light'>Cross Platform</b>";
$b_multiplayer = "<b class='text-light'>Multiplayer</b>";
$b_guide  = "<b class='text-light'>Story </b>";
$b_story  = "<b class='text-light'>Guide </b>";
$b_pvp  = "<b class='text-light'>PVP </b>";
$b_pve  = "<b class='text-light'>PVE </b>";
$b_game  = "<b class='text-light'> Game </b>";
$b_classes  = "<b class='text-light'>Classes</b>";
$b_players  = "<b class='text-light'>Players</b>";
$b_mmo  = "<a class='text-warning' href='/mmo-games'> MMO </a>";
$b_r2games = "<b class='text-light'>R2Games</b>";
$b_gtarcade = "<b class='text-light'>GTArcade</b>";
$b_upjers = "<b class='text-light'>Upjers</b>";
$b_101xp = "<b class='text-light'>101XP</b>";
$b_gameforge = "<b class='text-light'>GameForge</b>";
@endphp

and this is the article code

<p>
            {!! str_ireplace(array("\r","\n\n","\n", $game_info->g_name, '3D','2D', 'cross platform', 'cross-platform', 'multiplayer', 'r2games', 'upjers', '101xp', 'gameforge', 'story', 'guide', 'classes', 'pvp', 'pve', ' game ', 'players', ' mmo ', 'gtarcade'),array('',"\n","</p>\n<p>", $s_gamename, $b_3d, $b_2d,  $b_crossplatform,$b_crossplatform, $b_multiplayer, $b_r2games, $b_upjers, $b_101xp, $b_gameforge, $b_story, $b_guide, $b_classes,  $b_pvp, $b_pve, $b_game, $b_players, $b_mmo, $b_gtarcade),trim($game_info->g_info,"\n\r")) !!}</p>
0 likes
3 replies
bugsysha's avatar

Move logic from blade file at least to controller or god forbid some service class.

ahmeddabak's avatar

You can use the view presenter design pattern to extract all the view logic to a view presenter class

Create a presenter class

class ArticlePresenter {

public $article

public function __construct($article){
    $this->article = $article;
}

//create a lookup table
$replace = [
    "\r" => '',
    "\n\n" => "\n"
    //......
];

public function tags() {

    return str_ireplace(array_keys($this->replace), array_values($this->replace), $the_variabel_you_want_to_replace)
}


//do some other view logic here

}

in the controller return to the view the presenter

return view('view_name',[
    'presenter' => new ArticlePresenter($article)
]);

and in your view

<p> {{$presenter->tags() }} </p>
jlrdw's avatar

A good start prior to learning patterns and that sort of thing, would be mastering some good MVC Principles.

And yes, it takes a while.

Please or to participate in this conversation.