<?php
// I have a string, something like this:
$string = '
Lorep ipsum <a href="http://www.example.com">example</a> lorem ipsum
lorem ipsum http://www.example.com/index.php?param1=val¶m2=val lorem ipsum
';
// I need to do some magick with preg_replace and get string like this:
$string = '
Lorep ipsum <a href="http://www.example.com" target="_blank">example</a> lorem ipsum
lorem ipsum <a href="http://www.example.com/index.php?param1=val¶m2=val" target="_blank">http://www.example.com/index.php?param1=val¶m2=val</a> lorem ipsum
';
?>
So basicly, I want to linkify URLs in text that are not wrapped in <a></a> and add target="_blank" to those that are.
Can anyone help me with this?
This will add the target:
This is a crude way of detecting URLs and making them into links (this is brittle):
First, I would use some XML/HTML processing library, to get text between tags, then using simple regex:
PHP validation/regex for URL
make all URLs as links.
this will add an extra
target=\"_blank\"
incase it is already set.this will make sure that only one
target="_blank"
is added in the URLexample:- http://www.phpliveregex.com/p/6qG