-2

I need to preg_match html to add link to text. Currently use:

'![a-z][a-z-]+://!i'

Example:

some text mylink.com
Become
some text <a href="mylink.com">mylink.com</a>

And that is ok. But I have already 'a href' in first code and need to skip. I need to change this preg_match to detect link with first empty space. Maybe:

'!^[a-z][a-z-]+://!i'

but this not work, any help?

Laky
  • 745
  • 2
  • 12
  • 25
  • there's a hole lotta questions even on SO here on how to replace those links, for example https://stackoverflow.com/questions/13105960/replacing-text-link-as-link-with-preg-replace Your regex is way too unspecific to correctly find intended-URLs. – Adrian Föder Nov 06 '17 at 11:05

1 Answers1

0
'/<a\s+(?:[^"'>]+|"[^"]*"|'[^']*')*href=("[^"]+"|'[^']+'|[^<>\s]+)/i'

or you use simpledom praser => http://simplehtmldom.sourceforge.net/

//from string
$html = str_get_html($links);

//or from webpage
$html = file_get_html('www.example.com');

foreach($html->find('a') as $link) {
    echo $link->href . '<br />';
}
timod
  • 585
  • 3
  • 13