正则表达式PHP,将所有链接与特定文本匹配

正则表达式PHP,将所有链接与特定文本匹配,php,regex,pattern-matching,html-parsing,Php,Regex,Pattern Matching,Html Parsing,我正在寻找一个PHP正则表达式,它可以将锚点与它上面的特定文本相匹配。例如,我希望获得带有文本mylink的锚,如: <a href="blabla" ... >mylink</a> 因此,它应该匹配所有锚点,但只有当它们包含特定文本时,它才应该匹配这些字符串: <a href="blabla" ... >mylink</a> <a href="blabla" ... >blabla mylink</a> <

我正在寻找一个PHP正则表达式,它可以将锚点与它上面的特定文本相匹配。例如,我希望获得带有文本mylink的锚,如:

<a href="blabla" ... >mylink</a>

因此,它应该匹配所有锚点,但只有当它们包含特定文本时,它才应该匹配这些字符串:

<a href="blabla" ... >mylink</a>

<a href="blabla" ... >blabla mylink</a>

<a href="blabla" ... >mylink bla bla</a>

<a href="blabla" ... >bla bla mylink bla bla</a>

但不是这个:

<a href="blabla" ... >bla bla bla bla</a>

因为这个不包含单词mylink

另外,这个不应该匹配:
“mylink是字符串”
,因为它不是锚定

有人知道吗

Thanx Granit

这应该是可行的(构建正则表达式字符串并插入您需要的任何字符串,而不是“mylink”)

]*>[^]*mylink[^]*
但不建议这样做。您应该使用HTML解析器来处理标记。Regex并不是实现这一点的合适工具。(如果您有包含“>”的链接,上述正则表达式将不起作用,尽管这可能很少见)

如果您只使用适当的换行符,我假定php不需要任何特殊的转义字符

在regexpal.com上测试

一些注释::
\s*-匹配可选空格
\s+-至少匹配一个空格/制表符和任何额外的可选空格
[^>]-匹配除“>”以外的任何字符
[^]-匹配除“”以外的任何字符

更新:对于与m/regex/

匹配的php,如果(preg_match('%]*>(.*mylink.*)%',$text,$regs),则转义“/”){
if (preg_match('%<\s*a\s+href="blabla"[^>]*>(.*mylink.*)<\s*/a>%', $text, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}
$result=$regs[1]; }否则{ $result=“”; }
$regs[0]
将保存完整的匹配项
$regs[1]
将保留a标记内的位,但它应该可以工作。

请尝试使用解析器:

/<a[^>]*>([^<]*mylink[^<]*)<\/a>/
require_once "simple_html_dom.php";

$data = 'Hi, I am looking for a regular expression in PHP which would match the anchor with a 
specific text on it. E.g I would like to get anchors with text mylink like: 
<a href="blabla" ... >mylink</a>

So it should match all anchors but only if they contain specific text So it should match t
hese string:

<a href="blabla" ... >mylink</a>

<a href="blabla" ... >blabla mylink</a>

<a href="blabla" ... >mylink bla bla</a>

<a href="blabla" ... >bla bla mylink bla bla</a>

but not this one:

<a href="blabla" ... >bla bla bla bla</a> Because this one does not contain word mylink.

Also this one should not match: "mylink is string" because it is not an anchor.

Anybody any Idea? Thanx Granit';

$html = str_get_html($data);

foreach($html->find('a') as $element) {
  if(strpos($element->innertext, 'mylink') === false) {
    echo 'Ignored: ' . $element->innertext . "\n";
  } else {
    echo 'Matched: ' . $element->innertext . "\n";
  }
}

下载
simple\u html\u dom.php
自:

注意属性值可以包含普通的
。当然,添加了免责声明。我可以继续添加href=“[^”]*“|”[^']”,但接下来您希望所有属性都允许>,然后我必须允许属性名称仅以字符开头,而不是以数字开头。这就是我说使用HTML解析器的原因。:DI get warning:warning:preg_match():未知修饰符“a”@Granit,您需要在正则表达式中转义
/
,或者使用不同的分隔符。但是,我的建议真的有什么问题吗?@Granit:使用HTML解析器。总有一天会更好。使用现有的基于sax的解析器捕获a标记,应该这样做。简单明了。+1注意属性值可以包含一个普通的
。请注意,属性值可以包含一个普通的
。的可能重复项的可能重复项
require_once "simple_html_dom.php";

$data = 'Hi, I am looking for a regular expression in PHP which would match the anchor with a 
specific text on it. E.g I would like to get anchors with text mylink like: 
<a href="blabla" ... >mylink</a>

So it should match all anchors but only if they contain specific text So it should match t
hese string:

<a href="blabla" ... >mylink</a>

<a href="blabla" ... >blabla mylink</a>

<a href="blabla" ... >mylink bla bla</a>

<a href="blabla" ... >bla bla mylink bla bla</a>

but not this one:

<a href="blabla" ... >bla bla bla bla</a> Because this one does not contain word mylink.

Also this one should not match: "mylink is string" because it is not an anchor.

Anybody any Idea? Thanx Granit';

$html = str_get_html($data);

foreach($html->find('a') as $element) {
  if(strpos($element->innertext, 'mylink') === false) {
    echo 'Ignored: ' . $element->innertext . "\n";
  } else {
    echo 'Matched: ' . $element->innertext . "\n";
  }
}
Matched: mylink
Matched: mylink
Matched: blabla mylink
Matched: mylink bla bla
Matched: bla bla mylink bla bla
Ignored: bla bla bla bla