File: /var/www/html/sarvodayahospital/app/PageInterlink.php
<?php
namespace App;
use App\Models\Interlink;
class PageInterlink {
static function interlink_filter($content, $firstrun=true)
{
global $protectblocks;
$content = preg_replace_callback('!(\<a[^>]*\>([^>]*)\>)!ims',function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
if($firstrun)
{
$content = preg_replace_callback('!(\<code\>[\S\s]*?\<\/code\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\[tags*\][\S\s]*?\[\/tags*\])!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\<img[^>]*\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\<h1[^>]*\>([^>]*)\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\<h2[^>]*\>([^>]*)\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\<h3[^>]*\>([^>]*)\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\<h4[^>]*\>([^>]*)\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\<h5[^>]*\>([^>]*)\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
$content = preg_replace_callback('!(\<h6[^>]*\>([^>]*)\>)!ims', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return '[block]'.(count($protectblocks)-1).'[/block]';
}, $content);
}
$keywords = Interlink::orderBy('Priority','ASC')->get();
foreach($keywords as $keyR){
if ($keyR->OpenInNewWindow) {
$openExternalWindow = 'target="_blank" rel="noopener"';
} else {
$openExternalWindow = '';
}
$ankerTag = '<a href="'.$keyR->Link.'" '.$openExternalWindow.'>'.$keyR->Keyword.'</a>';
//$changedText = preg_replace('/('.preg_quote($keyR->keyword, '/').')/i', $ankerTag, $changedText,$keyR->count); // change by character
$content = preg_replace(sprintf('/\b%s\b/',preg_quote($keyR->Keyword)), $ankerTag, $content,$keyR->Count); //change by word
}
$content = preg_replace_callback('!(\[block\]([0-9]*?)\[\/block\])!', function ($blocks){
global $protectblocks;
$protectblocks[] = $blocks[1];
return $protectblocks[$blocks[2]];
}, $content);
return $content;
}
public static function interlink_formatter(string $html = null)
{
return static::interlink_filter($html);
}
}