Forum Moderators: coopster

Message Too Old, No Replies

Getting Title From HTML Links

         

brendan3eb

3:03 am on Mar 29, 2005 (gmt 0)

10+ Year Member



I'm trying to get the title from a string with an html link using this code:
<?php
$string = "<a href=/test>test link</a>";
$get_title = ereg_replace('<a href=[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]+>([[:alpha:]])+</a>', '\1', $string);
echo $string;
?>

however, it just spits out the same HTML link, a bit of help please?

ironik

5:08 am on Mar 29, 2005 (gmt 0)

10+ Year Member



try replacing the \1 to \2. The first var usually stores the entire match, by the look of it you want the first 'sub match'. I'm not entirely familiar with the syntax for the expression (normally use perl regulars), but I think that should work.

brendan3eb

3:56 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



tried that and it didn't work, thanks for the suggestion though.

coho75

4:00 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



How about something like this?

<?php
$string = "<a href=/test>test link</a>";
echo strip_tags($string);
?>

brendan3eb

4:04 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



that worked! thanks. I found the problem on the first code, I did have it right with /1, but you have to type the link in perfect form to get the title.

jamie

4:04 pm on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi,

something like

'<a href=[^>]+>(.*)<\/a>'

the [^>] matches anything except the closing bracket of the <a href> tag.

<added> too late - love the simple answer coho .. lol