Skip to content

Instantly share code, notes, and snippets.

@rocketgeek
Created August 4, 2021 14:10
Show Gist options
  • Save rocketgeek/6dffe495b6f516836dd33aa4291ab89b to your computer and use it in GitHub Desktop.
Save rocketgeek/6dffe495b6f516836dd33aa4291ab89b to your computer and use it in GitHub Desktop.
Extract "data-" attributes from WooCommerce HTML tags
<?php
// Extraction utility for getting attributes from HTML tag.
function extract_html_atts( $string, $prefix = "data-" ) {
$start = 0;
$end = 0;
while( strpos( $string, $prefix, $end ) ) {
$start = strpos( $string, $prefix, $start )+strlen( $prefix );
$end = strpos( $string, '"', $start )-1;
$end2 = strpos( $string, '"', $end+2 );
$array[ substr( $string, $start, $end-$start ) ] = substr( $string, $end+2, $end2-$end-2 );
}
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment