【问题标题】:preg_split regex helppreg_split 正则表达式帮助
【发布时间】:2011-08-13 23:27:03
【问题描述】:

在下面的processPage 函数中,我从每个处理过的 URL 的关键字元标记中获取关键字。我需要更改 preg_split 以便它只提取任何关键字集群的前三个单词。

例如,给定这个关键字meta标签:

<meta name="keywords" content="this is too long, this is not, keyword three" />

我只想要第一个关键字集群的“这也是”部分。

另外,如果关键字词组的总列表长于 10 个,我只想从列表中提取前 10 个关键字词组。

即,(关键字词组 1、kw 2、kw 3、kw4 等...,关键字词组 10)

非常感谢任何帮助。

<?php

class ResultPage
{
    function __construct($siteurl){$this->url = $siteurl;$this->processPage();}

    public $url;
    public $title;
    public $html;
    public $plainText;
    public $wordList;
    public $keywords = array();

    function processPage(){
        $this->html = rseo_keywordSearch_scrapePage($this->url);
        $dom = str_get_html($this->html);
        $metakws = $dom->find('meta[name=keywords]');
        if(count($metakws)){
            $metakw = $metakws[0];
            if($metakw->content){
                $this->keywords = preg_split("/[\s]*[,][\s]*/",$metakw->content); //EDIT HERE
                }
            }
        }

    public function GetResults(){
        return rseo_keyword_getCountArray($this->wordList);
    }
}


/*
 * 
 * Calls remote web page using cUrl, 
 * and returns the raw html
 * 
 */
function rseo_keywordSearch_scrapePage($url, $headonly = TRUE ){

    $agents = 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $agents);
    //curl_setopt($ch, CURLOPT_NOBODY, $headonly);
    curl_setopt($ch, CURLOPT_URL, $url);

    $curlResp = curl_exec($ch);
    curl_close($ch);
    $resp = str_replace("class=l","class='l'",$curlResp);

    return $resp;
}

function rseo_keyword_getCountArray($arr){
    $retarr = array_count_values($arr);
    arsort($retarr);
    return $retarr;
}

【问题讨论】:

    标签: php regex preg-split


    【解决方案1】:

    这比拆分更容易匹配,例如:

    preg_match_all('/(?<=^|,)\s*((?:[^\s,]+\s*){1,3})/', $metakw->content, $m);
    $this->keywords = array_slice($m[1], 0, 10);
    
    print_r($this->keywords);
    
    /*
    Array
        (
            [0] => this is too 
            [1] => this is not
            [2] => keyword three
        )
    */
    

    【讨论】:

    • +1 效果很好。喜欢它!如果我想跳过任何超过 3 个单词的关键字集群怎么办?
    • @Scott 它使正则表达式更加复杂,断言要求每个匹配项后跟逗号或字符串结尾:'/(?&lt;=^|,)\s*((?:[^\s,]+\s*){1,3})(?=\s*(?:,|$))/'
    • 甜蜜!非常感谢您的帮助。效果很好。
    【解决方案2】:

    Preg_split 不适合您尝试做的事情。

    我会尝试这样的事情:

    $keywords = explode(',', $this->content);
    
    foreach ($keywords as $key => $keyword) {
        $count = substr_count($keyword, ' ');
    
        if ($count > 2) {
            // first 3 words out of a keyword cluster.
            $this->keywords[] = implode(' ', explode(' ', $keyword, -($count - 2)));
        } else {
            $this->keywords[] = $keyword;
        }
    
        // stop a 10 keywords
        if ($key + 1 == 10) {
            break;
        }
    }
    

    【讨论】:

    • 我无法让这个例子工作。仍在尝试。如果我想跳过任何超过 3 个单词的关键字集群怎么办?
    猜你喜欢
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2011-06-10
    相关资源
    最近更新 更多