PHP implementation of the article keyword Plus link method

Source: Internet
Author: User
    1. $keywordArray = Array (
    2. Array (' process ', ' http://www.01happy.com/linux-ps-view-process/')
    3. , Array (' daemon ', ' http://www.01happy.com/linux-python-daemon/')
    4. );
Copy Code

Two-dimensional array ordering can use the built-in function Usort, the code is as follows:

    1. Usort ($keywordArray, function ($a, $b) {
    2. $al = strlen ($a [0]);
    3. $BL = strlen ($b [0]);
    4. if ($al = = $BL)
    5. return 0;
    6. Return ($al > $bl)? -1:1;
    7. });
Copy Code

Extract all the HTML tags

    1. Preg_match_all ('/<\/?[ a-za-z]+[^>]*>/', $content, $match);
    2. if (isset ($match [0])) {
    3. $htmlTagArray = $match [0];
    4. }
Copy Code

This side of the regular is the <> package of content are extracted, I think that all need to be the <> as content display, should be replaced with the escape character <&gt, for all <> wrapped content can be considered a label. In fact browsers do the same, and many online text editors automatically escape these characters.

split content, save to array $noTagContentArray = Preg_split ('/<\/?[ a-za-z]+[^>]*>/', $content);

The regular expression used here is the same as the regular formula used to extract the HTML tags above.

loop partition of the content array, replace the keyword link this way is to first replace the keyword to MD5 value, and then replace the MD5 value with a link keyword, the reason for this processing, is to solve the case between the keywords. In addition, the author has considered splitting the contents of the array implode into a string, do not have to replace the loop, but worried that the string after the implode will form a keyword in the stitching, so it does not deal with it.

combined extraction of HTML tags and processed content This step is relatively simple, stitching in turn OK. It is important to note that the HTML tag array is 1 smaller than the length of the segmented content array, so use Isset to determine the label. The above content has been tested many times no problem, if in doubt, welcome to the QQ Group of the site: 161228069 communication.

  • Related Article

    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.