tags:

views:

57

answers:

4

could someone help me with the regex pattern that i could use to match the text "Cats" within the link element?

<A HREF="http://www.catingale2?subject=10023"&gt;Cats&lt;/A&gt; forum

Thanks in advance!

+3  A: 

What about something like this :

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

Which gives :

string(4) "Cats" 


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

Pascal MARTIN
what function could i use then to get the text within elements?
weng
I you have a full portion of HTML *(something bigger than what you posted here, I mean)*, a "better" way, in many cases, would be to use an actual HTML parser ; like http://www.php.net/manual/en/domdocument.loadhtml.php and then go through the DOM, possibly using XPath queries.
Pascal MARTIN
+2  A: 

no need regex

$str=<<<EOF
<A HREF="http://www.catingale2?subject=10023"&gt;Cats&lt;/A&gt; forum
<A HREF="http://www.catingale2?subject=10024"&gt;
 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";
    }
}

output

# php test.php
Cats

 Dogs
ghostdog74
+2  A: 
<a[^>]*>([^<]*)<\/a>

That's it. You could probably get away with:

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

or

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

The regex used is: <a.*?>(.*?)</a>

Explanation:

<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
codaddict