From The Blog

PHP: Regex match anchor text and url in a string

12 November 2013, in

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);



Post a Comment

PHP: check string has HTML tags

25 November 2012, in

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';
}



Post a Comment