Last active
September 6, 2017 09:41
-
-
Save lastguest/4986443 to your computer and use it in GitHub Desktop.
[Text-level HTML Manipulation functions] #PHP #HTML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function extractByClass($class,$context){ | |
| preg_match_all('#<\s*(\w+)\s*class="\s*'.$class.'\s*"[^>]*>(.*?)</\s*\g1\s*>#sm',$context,$matches); | |
| return $matches[2]; | |
| } | |
| function extractByID($class,$context){ | |
| preg_match_all('#<\s*(\w+)\s*id="\s*'.$class.'\s*"[^>]*>(.*?)</\s*\g1\s*>#sm',$context,$matches); | |
| return $matches[2]; | |
| } | |
| function extractByTag($tag,$context){ | |
| preg_match_all('#<\s*'.$tag.'[^>]*>(.*?)</\s*'.$tag.'\s*>#sm',$context,$matches); | |
| return $matches[1]; | |
| } | |
| function extractAttribute($attr,$context){ | |
| preg_match_all('#'.$attr.'\s*=\s*[\'\"]([^\'\"]+)[\'\"]#sm',$context,$matches); | |
| return $matches[1]; | |
| } | |
| function hasAttribute($attr,$context){ | |
| return preg_match('#'.$attr.'\s*=#',$context); | |
| } | |
| function createAttribute($attr,$val,$context){ | |
| return preg_replace('#>#',' '.$attr.'="'.$val.'">',$context,1); | |
| } | |
| function addToAttribute($attr,$val,$context){ | |
| return hasAttribute($attr,$context) | |
| ?preg_replace('#'.$attr.'\s*=\s*([\'\"])([^\'\"]+)[\'\"]#',$attr.'=\1'.$val.' \2\1',$context) | |
| :createAttribute($attr,$val,$context); | |
| } | |
| function removeFromAttribute($attr,$val,$context){ | |
| return preg_replace('#'.$attr.'\s*=\s*([\'\"])('.$val.')([^\'\"]+)[\'\"]#',$attr.'=\1\3\1',$context); | |
| } | |
| function findByClass($class,$context){ | |
| preg_match_all('#<\s*(\w+)\s*class\s*=["\']\s*'.$class.'\s*["\'][^>]*>(.*?)</\s*\g1\s*>#sm',$context,$matches); | |
| return $matches[0]; | |
| } | |
| function findByID($class,$context){ | |
| preg_match_all('#<\s*(\w+)\s*id\s*=["\']\s*'.$class.'\s*["\'][^>]*>(.*?)</\s*\g1\s*>#sm',$context,$matches); | |
| return $matches[0]; | |
| } | |
| function findByTag($tag,$context){ | |
| preg_match_all('#<\s*'.$tag.'[^>]*>(.*?)</\s*'.$tag.'\s*>#sm',$context,$matches); | |
| return $matches[0]; | |
| } | |
| function findByAttribute($attr,$val,$context){ | |
| if($val===null) $val = '[^\'\"]+'; // Match all | |
| preg_match_all('#<\s*(\w+)\s*'.$attr.'\s*=["\']\s*'.$val.'\s*["\'][^>]*>(.*?)</\s*\g1\s*>#sm',$context,$matches); | |
| return $matches[0]; | |
| } | |
| function extractText($context){ | |
| return preg_replace('#<[^>]*>#m','',$context); | |
| } | |
| function getInnerHTML($context){ | |
| return substr($context, strpos($context,'>')+1 , -(strlen($context)-strrpos($context,'<')) ); | |
| } | |
| function setInnerHTML($html,$context){ | |
| return substr($context, 0, strpos($context,'>')+1) . $html . substr($context, -(strlen($context)-strrpos($context,'<')) ); | |
| } | |
| function removeComments($context){ | |
| return preg_replace('/<!--(.*)-->/Us','', $context); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment