PHP matching character link address program code

Source: Internet
Author: User

Determine if a string contains hyperlinks

The code is as follows

$str = "Ssdsf<a target= ' _blank ' href= ' http://www.45it.net/' >SDF</A>SDFSS '";
if (Preg_match ("/<a/s+href=[^>]*>|<//[^a]*a[^>]*>/i", $str))
{
echo "This string has a hyperlink";
}
Else
{
echo "This string has no hyperlink tag";
}
?>


Now we just need to filter the connection part.

The code is as follows

<?php
echo preg_replace ("/<=href=) ([^>]*) (?=>)/I", "#", "<a href= ' www.45it.net ' > Hello, click here to see </a><a href= ' www.45it.net ' > Hello, click here to see </a> ');
?>

Regular:/(? <=href=) ([^>]*) (?=>)/

(? <=exp) matches the position of the exp rear
(? =exp) match the position of the exp front
This matches all characters that are not ">" before ">" After href=

Example: <a href= ' www.45it.net ' >

Find these characters (URLs) with the # replacement, you can remove all the links in HTML.

Now share an instance of extracting a super connection

  code is as follows &nbs P;
Function Match_links ($document) {   

 & nbsp;  preg_match_all ("' <s*as.*?hrefs*=s*")? (1) (.*?) 1| ([^s>]+)] [^>]*>? (.*?) </a> ' Isx ', $document, $links);             &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP

    while (list $key, $ val) = each ($links [2])) {

        if (!empty ($val))

             $match [' link '] = $val;

   }

    while (list ($key, $val) = each ($links [3])) {

        if (!empty ($val))

            $match[' link ' [] = $val;

   }       

    while ($key, $val) = each ($links [4])) {

        if (!empty ($val))

             $match [' content '] = $val;

   }

    while (list ($key, $val) = each ($links [0])) {

        if (!empty ($val))

            $match [' All '] [] = $val;

   }                

    return $match

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.