Skip to content

Instantly share code, notes, and snippets.

@andreiglingeanu
Created May 9, 2019 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiglingeanu/8e3afa19cfb2394bc21e502bed12e47c to your computer and use it in GitHub Desktop.
Save andreiglingeanu/8e3afa19cfb2394bc21e502bed12e47c to your computer and use it in GitHub Desktop.
<?php
class Parser {
public function add_attribute_to_images(
$content,
$attribute_name,
$attribute_value
) {
$new_content = $this->add_attribute_to_images_with_tag(
$content,
$attribute_name,
$attribute_value,
'img'
);
return $this->add_attribute_to_images_with_tag(
$new_content,
$attribute_name,
$attribute_value,
'source'
);
}
public function remove_attribute_from_images($content, $attribute) {
$new_content = $this->remove_attribute_from_images_with_tag(
$content,
$attribute,
'img'
);
return $this->remove_attribute_from_images_with_tag(
$new_content,
$attribute,
'source'
);
}
public function rename_attribute_from_images(
$content,
$old_attribute_name,
$new_attribute_name
) {
$new_content = $this->rename_attribute_from_images_with_tag(
$content,
$old_attribute_name,
$new_attribute_name,
'img'
);
return $this->rename_attribute_from_images_with_tag(
$new_content,
$old_attribute_name,
$new_attribute_name,
'source'
);
}
private function rename_attribute_from_images_with_tag(
$content,
$old_attribute_name,
$new_attribute_name,
$tag = 'img'
) {
if (!preg_match_all('/<' . $tag . ' [^>]+>/', $content, $matches)) {
return $content;
}
$selected_images = array();
foreach ($matches[0] as $image) {
// Maybe decide here better on which image to process
// if (preg_match('/attachment-ct-image-size/', $image, $class_id)) {
// }
$selected_images[] = $image;
}
foreach ($selected_images as $image) {
$content = str_replace(
$image,
$this->rename_attribute_for_single_image(
$image,
$old_attribute_name,
$new_attribute_name,
$tag
),
$content
);
}
return $content;
}
/**
* @param string $content
* @param string $attribute_name
* @param string $attribute_value
* @param string $tag img | source
*/
private function add_attribute_to_images_with_tag(
$content,
$attribute_name,
$attribute_value,
$tag = 'img'
) {
if (!preg_match_all('/<' . $tag . ' [^>]+>/', $content, $matches)) {
return $content;
}
$selected_images = array();
foreach ($matches[0] as $image) {
// Maybe decide here better on which image to process
// if (preg_match('/attachment-ct-image-size/', $image, $class_id)) {
// }
$selected_images[] = $image;
}
foreach ($selected_images as $image) {
$content = str_replace(
$image,
$this->add_attribute_to_single_image(
$image,
$attribute_name,
$attribute_value,
$tag
),
$content
);
}
return $content;
}
/**
* Remove attribute from a specific HTML tag.
*
* @param string $content
* @param string $attribute
* @param string $tag - img | source
*/
private function remove_attribute_from_images_with_tag(
$content,
$attribute,
$tag = 'img'
) {
if (!preg_match_all('/<' . $tag . ' [^>]+>/', $content, $matches)) {
return $content;
}
$selected_images = array();
foreach ($matches[0] as $image) {
// if (preg_match('/attachment-ct-image-size/', $image, $class_id)) {
// }
$selected_images[] = $image;
}
foreach ($selected_images as $image) {
$content = str_replace(
$image,
$this->remove_attribute_from_single_image(
$image,
$attribute,
$tag
),
$content
);
}
return $content;
}
/**
* Remove existing $attribute from <img> html, if it exists.
*
* @param string $image - image HTML
* @param string $attribute - attribute name
* @param string $tag - img | source
*/
private function remove_attribute_from_single_image(
$image,
$attribute,
$tag = 'img'
) {
return preg_replace(
'/(\\<' .
$tag .
'[^>]+)(\\s?' .
$attribute .
'\\="[^"]+"\\s?)([^>]+)(>)/',
'${1}${3}${4}',
$image
);
}
private function rename_attribute_for_single_image(
$image,
$old_attribute_name,
$new_attribute_name,
$tag = 'img'
) {
$old_attribute_value = ltrim(
rtrim(
trim(
preg_replace(
'/(\\<' .
$tag .
'[^>]+)(\\s?' .
$old_attribute_name .
'\\="[^"]+"\\s?)([^>]+)(>)/',
'${2}',
$image
)
),
'"'
),
$old_attribute_name . '="'
);
$removed = $this->remove_attribute_from_images(
$image,
$old_attribute_name
);
$res = $this->add_attribute_to_single_image(
$removed,
$new_attribute_name,
$old_attribute_value,
$tag
);
return $res;
}
/**
* Add an attribute with a specific value an img element. Remove the
* attribute if it exists already.
*
* @param string $image - image HTML
* @param string $attribute_name
* @param string $attribute_value
* @param string $tag - img | source
*/
private function add_attribute_to_single_image(
$image,
$attribute_name,
$attribute_value,
$tag = 'img'
) {
$attr = sprintf(
' %s="%s"',
esc_attr($attribute_name),
esc_attr($attribute_value)
);
$val = preg_replace(
'/<' . $tag . ' ([^>]+?)[\\/ ]*>/',
'<' . $tag . ' $1' . $attr . ' />',
$this->remove_attribute_from_images($image, $attribute_name)
);
return $val;
}
}
// Usage:
$parser = new Parser();
$output = $parser->add_attribute_to_images(
$html,
'sizes',
'attribute_value'
);
$output = $parser->remove_attribute_from_images(
$html,
'sizes',
);
$output = $parser->rename_attribute_from_images(
$html,
'sizes',
'lazy-sizes'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment