- $keywordArray = Array (
- Array (' process ', ' http://www.01happy.com/linux-ps-view-process/')
- , Array (' daemon ', ' http://www.01happy.com/linux-python-daemon/')
- );
Copy CodeTwo-dimensional array ordering can use the built-in function Usort, the code is as follows:
- Usort ($keywordArray, function ($a, $b) {
- $al = strlen ($a [0]);
- $BL = strlen ($b [0]);
- if ($al = = $BL)
- return 0;
- Return ($al > $bl)? -1:1;
- });
Copy CodeExtract all the HTML tags
- Preg_match_all ('/<\/?[ a-za-z]+[^>]*>/', $content, $match);
- if (isset ($match [0])) {
- $htmlTagArray = $match [0];
- }
Copy CodeThis 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 <>, 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. |