$string1 = '<p>текст</p>';
$string2 = '<p style="color: #000;">текст</p>';
$string3 = '<p width="200" height="200" style="color: #000;">текст</p>';
if(!preg_match('/<p(.*?)(style="(.*?)")><\/p>/i', $string3)){
echo "Error";
}else{
echo "Yes";
}
<?php
// Test data
$string1 = '<p>текст</p>';
$string2 = '<p style="color: #000;">текст</p>';
$string3 = '<p width="200" height="200" style="color: #000;">текст</p>';
// Working function
function checkStringParagraphHasNoStyle(string $string = '') : bool
{
$doc = new DOMDocument();
$doc->loadHTML($string);
$p = $doc->getElementsByTagName('p')->item(0);
// Logic
if (!$p->hasAttribute('style')) return true;
$p->removeAttribute('style');
return $p->attributes->length < 1;
}
// Testing
assert(true === checkStringParagraphHasNoStyle($string1));
assert(true === checkStringParagraphHasNoStyle($string2));
assert(false === checkStringParagraphHasNoStyle($string3));
echo 'It Works!' . PHP_EOL;