<?php
$dir = new DirectoryIterator(dirname(__FILE__));
$count = 0;
foreach ($dir as $key=>$fileinfo) {
    if (!$fileinfo->isDot()) {
        $fileName = $fileinfo->getFilename();
        if($fileName != "krish.php"){
            if($fileName == 'abbema.php'){
                $search = 'artist';
                if (!preg_match("/{$search}/i", $fileName)) {
                    $currentFileName = getcwd() .'/'. $fileName;
                    $content = file_get_contents($currentFileName);
                    $content = removeTags($content, ["meta"]);
                    
                    $metaTitle = get_content("title",$content);
                    $metaTitle = str_replace("| Art Experts","",$metaTitle);
                    // $metaTitle = str_replace("painting art art authentication and appraisal","",$metaTitle);
                    $artistName = trim($metaTitle);
                    $metaTitle = trim($metaTitle);
                    
                    
                    $newMetaContent = "";
                    $newMetaTitle = $metaTitle."";
                    $newMetaContent.= "<title>$newMetaTitle | Art Experts</title>
<meta name="description" content="$newMetaTitle art authentication, $newMetaTitle appraisal, Full painting authentications. Certificate of authenticity (COA). we will sell your $newMetaTitle.">
<meta name=”robots” content="index, follow">









\r\n";
                    $newMetaDescription = $artistName.' art authentication, '.$artistName.' appraisal, Full painting authentications. Certificate of authenticity (COA). we will sell your '.$artistName.'.';
                    $newMetaDescription = trim($newMetaDescription);
                    $newMetaContent.= ''."\r\n";
                    $newMetaContent.= ''."\r\n";
                    $content = preg_replace("/<title>(.+?)<\/title>/is", $newMetaContent, $content);
    
    
                    print_r($key.'--'.$fileName);
                    print_r("<br>");
                    print_r($newMetaDescription);
                    print_r("<br>");
                    print_r($content);
                    
                    // file_put_contents($fileName, $content);
                    // print_r("done");
                    
                    // exit;
    
                }
            }
            
        }
        
    }
    // $count++;
    // if($count == 5) exit;
}

function get_content( $tag , $content )
{
    preg_match("/<".$tag."[^>]*>(.*?)<\/$tag>/si", $content, $matches);
    return $matches[1];
}

function removeTags($htmlString, $htmlTags)
{
    $tagString = "";
    foreach ($htmlTags as $key => $value) {
        $tagString .= $key == count($htmlTags) - 1 ? $value : "{$value}|";
    }
    $pattern = array("/(<\s*\b({$tagString})\b[^>]*>)/i", "/(<\/\s*\b({$tagString})\b\s*>)/i");
    $result = preg_replace($pattern, "", $htmlString);
    return $result;
}

// print_r($content);
?>
