regex deseni ile yardım

4 Cevap php

Birisi i link elemanı içindeki metni "Cats" eşleştirmek için kullanabilirsiniz regex deseni ile bana yardımcı olabilir?

<A HREF="http://www.catingale2?subject=10023">Cats</A> forum

Şimdiden teşekkürler!

4 Cevap

Ne bu böyle bir şey:

$str = '<A HREF="http://www.catingale2?subject=10023">Cats</A> forum';
$matches = array();
if (preg_match('#<a[^>]*>(.*?)</a>#i', $str, $matches)) {
    var_dump($matches[1]);
}

Verir:

string(4) "Cats" 


And, as a sidenote : generally speaking, using regex to "parse" HTML is not quite a good idea !

gerek regex

$str=<<<EOF
<A HREF="http://www.catingale2?subject=10023">Cats</A> forum
<A HREF="http://www.catingale2?subject=10024">
 Dogs</A>
forum
EOF;
$s = explode("</A>",$str);
foreach($s as $v){
    if (strpos($v,"<A HREF")!==FALSE){
       $t=explode(">",$v);
       print end($t)."\n";
    }
}

çıktı

# php test.php
Cats

 Dogs
<a[^>]*>([^<]*)<\/a>

İşte bu. Muhtemelen ile alabilir:

<a[^>]*>(.*)<\/a>

veya

<a[^>]*>([^<]*)
$input = '<A HREF="http://www.catingale2?subject=10023">Cats</A> forum';
if(preg_match('{<a.*?>(.*?)</a>}i',$input,$matches)) {
    $hyperlinked = $matches[1];
}
echo $hyperlinked; // print Cats

Kullanılan regex olduğunu: <a.*?>(.*?)</a>

Açıklama:

<a.*?> - an opening anchor tag with any attribute.
(.*?)  - match and remember between opening anchor tag and closing anchor tag.
</a>   - closing anchor tag.
i      - to make the entire matching case insensitive