<?php
$dir = new DirectoryIterator(dirname(__FILE__));
$count = 0;
foreach ($dir as $key=>$fileinfo) {
    if (!$fileinfo->isDot()) {
        $fileName = $fileinfo->getFilename();
        if($fileName != "harding_copy.php"){
            // if($fileName == 'arke_copy.php'){
            if(1){
                $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</title>\r\n";
                    $newMetaDescription = $artistName.' art authentication, '.$artistName.' appraisal, Full painting authentications. Certificate of authenticity (COA). we will sell your '.$artistName.'.';
                    $newMetaDescription = trim($newMetaDescription);
                    $newMetaContent.= '<meta name="description" content="'.$newMetaDescription.'">'."\r\n";
                    $newMetaContent.= '<meta name=”robots” content="index, follow">'."\r\n";
                    
                    // $content = preg_replace('/<p class="wow fadeInLeft krish" data-wow-delay=".4s">(.+?)<\/p>/is', $newMetaContent, $content);

                    $oldContent = 'Do you think you may own a painting by '.$artistName.'?
                    We perform <a href="https://www.artexpertswebsite.com/authentication.php">art
                        art authentications</a>, <a
                        href="https://www.artexpertswebsite.com/appraisal/painting-appraisal.php">art
                        appraisals</a>, <a
                        href="https://www.artexpertswebsite.com/authentication/provenance-research.php">art
                        research</a> and provide <a
                        href="https://www.artexpertswebsite.com/authentication/certificate-of-authenticity.php">Certificates
                        of Authenticity</a> (COA) as well as consultations, for all artworks by '.$artistName.'';

                    $newContent = '<p class="wow fadeInLeft" data-wow-delay=".4s">'.$artistName.' art authentication from \$175. <a href="https://www.artexpertswebsite.com/appraisal/painting-appraisal.php">'.$artistName.' appraisal</a>. <a href="https://www.artexpertswebsite.com/authentication/certificate-of-authenticity.php">'.$artistName.' certificates of authenticity (COA)</a>.  '.$artistName.' analysis, <a href="https://www.artexpertswebsite.com/authentication/provenance-research.php">research</a>, <a href="https://www.artexpertswebsite.com/authentication/scientific-tests.php">scientific tests</a>, <a href="https://www.artexpertswebsite.com/authentication.php">full art authentications</a>. We will help you sell your '.$artistName.' or we will sell it for you.</p>';
                    // $content = str_replace($oldContent,$newContent,$content);

                    
                    $content = preg_replace('/<p class="wow fadeInLeft krish" data-wow-delay=".4s">(.+?)<\/p>/is', $newContent, $content);

    
                    print_r($key.'--'.$fileName);
                    // print_r("<br>");
                    // print_r($oldContent);
                    // print_r("<br>");
                    // print_r($newContent);
                    // print_r("<br>");
                    print_r($content);
                    print_r("<br>");
                    
                    // file_put_contents($fileName, $content);
                    print_r("done");
                    echo '<br>';
                    
                    // 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);
?>
