Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. /**
  2.      * Converts a img tag to CSS style
  3.      *
  4.      * @access public
  5.      * @param <type> $img
  6.      * @return string css or empty string
  7.      */
  8.     public function convert_img_to_css( $img ) {
  9.         if ( !is_string( $img ) && false !== stripos( '<img', $img ) ) {
  10.             throw new Exception ( __METHOD__ . ' excpects a string containing a <img /> tag. Recieved a ' . gettype( $img ) );
  11.         }
  12.  
  13.         /**
  14.          * Match the img
  15.          */
  16.         preg_match( '/<img[^>]+/i', $img, $match );
  17.  
  18.         if ( sizeof( $match ) === 0 ) {
  19.             throw new Exception ( __METHOD__ . ' first argument did not contain a valid <img /> tag' );
  20.         }
  21.  
  22.  
  23.         preg_match_all( '/([a-z]+)="(.*?)"/i', $match[0], $attr );
  24.  
  25.         $allowed = array(
  26.             'src'       => 'background: url("%s") no-repeat top left;',
  27.             'width'     => 'width: %spx;',
  28.             'height'    => 'height: %spx;',
  29.         );
  30.  
  31.         $style = '';
  32.  
  33.         /**
  34.          * Fetch the corresponding HTML tag and add it to the style
  35.          */
  36.         for ( $i = 0; $i < sizeof( $attr[1] ); $i++ ) {
  37.             /**
  38.              * Check if its tags that might be relevant to our conversion
  39.              */
  40.            
  41.             if ( array_key_exists($attr[1][$i], $allowed) ) {
  42.                 printf($allowed[$attr[1][$i]],$attr[2][$i]);
  43.                 //$style .= sprintf( $allowed[$attr[1][$i]], $attr[2][$i] );
  44.             }
  45.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement