tags:

views:

39

answers:

3

i want to get the word Asarum in the below line:

 <A HREF="http://www.getty.edu/vow/TGNFullDisplay?find=&amp;place=&amp;nation=&amp;english=Y&amp;subjectid=1062783"&gt;Asarum&lt;/A&gt;

i tried with:

preg_replace('/<a.*?>/i', '', $link);
preg_replace('/<\/a>/i', '', $link);
echo $link;

but it doesn't work. nothing was cut out.

could someone help me?

+1  A: 

You need to assign the result:

$link = preg_replace('/<a.*?>/i', '', $link);
$link = preg_replace('/<\/a>/i', '', $link);
echo $link;
KennyTM
strange. i have never done that before and it seemed to work...maybe i am wrong.
weng
+1  A: 

Use this code, to match the text to a specific regex, instead of replacing everything else:

preg_match('/<a.*?>(.+?)<\/a>/i', $link, $matches);

$matches[1] will now contain the text you're looking for.

Douwe Maan
are you sure? i just want to get the text within the tags, not the whole element which seems to be that im getting with your code.
weng
My code should give you just the text inside the `<a>` tag... Have you tried `print_r($matches)` to see what's exactly in there?
Douwe Maan
+1 because your solution does work, though with the given input you could get away with `'/>([^<]+)/'` as the pattern.
mrclay
+3  A: 

Fastest (probably - not tested):

$t = substr($link, strpos($link, '>') + 1, -4);

Clearest:

$t = strip_tags($link);

Basic strip tags w/ regex:

$t = preg_replace('/<[^>]*>/', '', $link);
mrclay
Short, simple, and reliable. +1
Jakob
beautiful solution=)
weng
I should note, this only works if the string you're given only contains that single HTML element. If you were trying to pick an anchor element out of a larger fragment of HTML, you'd want DouweM's solution.
mrclay