3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $mysql_result[0]['id']="1"; $mysql_result[1]['id']="2"; $mysql_result[2]['id']="3"; $mysql_result[0]['address']="www.example.com/page1"; $mysql_result[1]['address']="www.example.com/page2"; $mysql_result[2]['address']="www.example.com/page3"; $mysql_result[0]['title']="Page 1"; $mysql_result[1]['title']="Page 2"; $mysql_result[2]['title']="Page 3"; $mysql_result[0]['content']="The quick dog jumps over the lazy dog."; $mysql_result[1]['content']="The best thing about morning is breakfast."; $mysql_result[2]['content']="Hotdogs are great ballpark food."; $mysql_result[0]['image']="image1.jpg"; $mysql_result[1]['image']="image2.jpg"; $mysql_result[2]['image']="image3.jpg"; $query="dog"; foreach($mysql_result as $results) { $content = str_replace($query,"my_un1qu3_r3pl4c3m3nt_".$query,$results['content']); $occurences = explode("my_un1qu3_r3pl4c3m3nt_",$content); foreach($occurences as $an_occurence) { if (strpos($an_occurence,$query) !== false) { $content=$an_occurence; echo htmlspecialchars('<li class="media"> <a class="pull-left" href="'.$results['address'].'"> <img class="media-object thumbnail" src="'.$results['image'].'" style="height:100px !important"> </a> <div class="media-body"> <h4 class="media-heading"><a href="'.$results['address'].'">'.$results['title'].'</a></h4> <p>...'.str_replace($query,'<strong>'.$query.'</strong>', substr($content,strpos($content,$query)-25,160)).'...</p> </div> </li>'); } } }
Output for 4.3.0 - 5.2.1, 5.2.7 - 7.1.0
&lt;li class=&quot;media&quot;&gt; &lt;a class=&quot;pull-left&quot; href=&quot;www.example.com/page1&quot;&gt; &lt;img class=&quot;media-object thumbnail&quot; src=&quot;image1.jpg&quot; style=&quot;height:100px !important&quot;&gt; &lt;/a&gt; &lt;div class=&quot;media-body&quot;&gt; &lt;h4 class=&quot;media-heading&quot;&gt;&lt;a href=&quot;www.example.com/page1&quot;&gt;Page 1&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;...&lt;strong&gt;dog&lt;/strong&gt; jumps over the lazy ...&lt;/p&gt; &lt;/div&gt; &lt;/li&gt;&lt;li class=&quot;media&quot;&gt; &lt;a class=&quot;pull-left&quot; href=&quot;www.example.com/page1&quot;&gt; &lt;img class=&quot;media-object thumbnail&quot; src=&quot;image1.jpg&quot; style=&quot;height:100px !important&quot;&gt; &lt;/a&gt; &lt;div class=&quot;media-body&quot;&gt; &lt;h4 class=&quot;media-heading&quot;&gt;&lt;a href=&quot;www.example.com/page1&quot;&gt;Page 1&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;...&lt;strong&gt;dog&lt;/strong&gt;....&lt;/p&gt; &lt;/div&gt; &lt;/li&gt;&lt;li class=&quot;media&quot;&gt; &lt;a class=&quot;pull-left&quot; href=&quot;www.example.com/page3&quot;&gt; &lt;img class=&quot;media-object thumbnail&quot; src=&quot;image3.jpg&quot; style=&quot;height:100px !important&quot;&gt; &lt;/a&gt; &lt;div class=&quot;media-body&quot;&gt; &lt;h4 class=&quot;media-heading&quot;&gt;&lt;a href=&quot;www.example.com/page3&quot;&gt;Page 3&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;... are great ballpark food....&lt;/p&gt; &lt;/div&gt; &lt;/li&gt;
Output for 5.2.2 - 5.2.6
&lt;li class=&quot;media&quot;&gt; &lt;a class=&quot;pull-left&quot; href=&quot;www.example.com/page1&quot;&gt; &lt;img class=&quot;media-object thumbnail&quot; src=&quot;image1.jpg&quot; style=&quot;height:100px !important&quot;&gt; &lt;/a&gt; &lt;div class=&quot;media-body&quot;&gt; &lt;h4 class=&quot;media-heading&quot;&gt;&lt;a href=&quot;www.example.com/page1&quot;&gt;Page 1&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;......&lt;/p&gt; &lt;/div&gt; &lt;/li&gt;&lt;li class=&quot;media&quot;&gt; &lt;a class=&quot;pull-left&quot; href=&quot;www.example.com/page1&quot;&gt; &lt;img class=&quot;media-object thumbnail&quot; src=&quot;image1.jpg&quot; style=&quot;height:100px !important&quot;&gt; &lt;/a&gt; &lt;div class=&quot;media-body&quot;&gt; &lt;h4 class=&quot;media-heading&quot;&gt;&lt;a href=&quot;www.example.com/page1&quot;&gt;Page 1&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;......&lt;/p&gt; &lt;/div&gt; &lt;/li&gt;&lt;li class=&quot;media&quot;&gt; &lt;a class=&quot;pull-left&quot; href=&quot;www.example.com/page3&quot;&gt; &lt;img class=&quot;media-object thumbnail&quot; src=&quot;image3.jpg&quot; style=&quot;height:100px !important&quot;&gt; &lt;/a&gt; &lt;div class=&quot;media-body&quot;&gt; &lt;h4 class=&quot;media-heading&quot;&gt;&lt;a href=&quot;www.example.com/page3&quot;&gt;Page 3&lt;/a&gt;&lt;/h4&gt; &lt;p&gt;... are great ballpark food....&lt;/p&gt; &lt;/div&gt; &lt;/li&gt;