删除具有特定单词preg_match和regex的行

时间:2017-01-18 00:28:43

标签: php regex domdocument preg-match-all

<a href="link">[BIOL 107 Section 1] Concepts in Biology</a></td>
<a href="link">[CENG 230 Section 7] Introduction to C Programming</a>
<a href="link">[CENG 230 All Sections] Introduction to C Programming</a></td>

以上示例是我的代码。

我正在尝试获取不包含所有anchors。我几乎尝试了所有东西,看了正则表达式文档,但我无法想出一些有用的东西。

2 个答案:

答案 0 :(得分:0)

试试这个正则表达式:

<a\s*[^>]*?>(?![^<]*?All).*?<\/a>

Demo

答案 1 :(得分:0)

请勿使用正则表达式解析HTML,请使用DOMDocument

.controller('verifyOrgCtrl', ['$rootScope', '$scope', '$state', '$http', 'dataServices', 'globalService', function ($rootScope, $scope, $state, $http, dataServices, globalService) {
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        window.history.forward();
    });
    $scope.orgList = [];
    dataServices.getOrgList().then(function (res) {
        if (res.status) {
            $scope.orgList = res.orgList;
        } else {
            $rootScope.serviceErrorMsg = res.error;
            $state.go(res.redirectURL);
        }
    });
    $scope.organization = {};
    $scope.validateOrg = function (formName) {
        $scope.formName = formName;
        if ($scope.formName.$invalid) {
            $scope.showErrorMsg = true;
            return;
        }
    }
}])

<强>输出:

$html = '
<a href="link1">[BIOL 107 Section 1] Concepts in Biology</a>
<a href="link2">[CENG 230 Section 7] Introduction to C Programming</a>
<a href="link3">[CENG 230 All Sections] Introduction to C Programming</a>
';
$dom = new DOMDocument;
$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('a');
$links = array();
$value = array();
foreach($tags as $a){
    if (preg_match('/\ball\b/i', $a->nodeValue)) continue;
    $links[] = $a->getAttribute('href');
    $value[] = $a->nodeValue;
}
print_r($links);
print_r($value);