PHP: Regex match anchor text and url in a string


To match anchor text and url in a string: $sourcestring = 'this is a good test <strong><a href="http://wordpress.org">file (.docx)</a></strong>  and this is the new <a href="wp-content/uploads/amir.jpg">site image</a> '; preg_match_all('/<a[^>]+href=[\'|"]([^>]+)[^>]+>([^<]+)<\/a>/i',$sourcestring,$matches); var_dump($matches);

PHP: check string has HTML tags


you may think as the best way to check a string has HTML tags is to check the string after stripping tags: if( strlen($string) != strlen(strip_tags($string)) ){ echo 'Contains HTML tags'; } But, the best way is to use regular expressions: if (preg_match("/([\<])([^\>]{1,})*([\>])/i", $string )) { echo 'Contains HTML tags'; }