Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is a sample tweet https://tr.co/sdfhnjn3
- This is a sample tweet <a href="https://tr.co/sdfhnjn3">https://tr.co/sdfhnjn3</a>
- $string = preg_replace('~(?<!href=['"])https?://[w/._-&?]*(?!</a>)(?=[^w/._-&])~gs', '<a href="$0">$0</a>', $string);`;
- $message = preg_replace('$(s|^)(https?://[a-z0-9_./?=&-]+)(?![^<>]*>)$i', ' <a href="$2" target="_blank">$2</a> ', $message." ");
- $message = preg_replace('$(s|^)(www.[a-z0-9_./?=&-]+)(?![^<>]*>)$i', '<a target="_blank" href="http://$2" target="_blank">$2</a> ', $message." ");
- $tweets = [
- 'This is a sample tweet https://tr.co/sdfhnjn3',
- 'This is a sample tweet <a href="https://tr.co/sdfhnjn3">https://tr.co/sdfhnjn3</a>',
- 'This is a sample tweet https://example.com/sdfhnjn3',
- 'This is a sample tweet <a href="https://example.com/sdfhnjn3">https://example.com/sdfhnjn3</a>'
- ];
- var_export(preg_replace('~<a [^>]+>.*?</a>(*SKIP)(*FAIL)|https?://(?:www)?tr.co/S*~', '<a href="$0">$0</a>', $tweets));
- array (
- 0 => 'This is a sample tweet <a href="https://tr.co/sdfhnjn3">https://tr.co/sdfhnjn3</a>',
- 1 => 'This is a sample tweet <a href="https://tr.co/sdfhnjn3">https://tr.co/sdfhnjn3</a>',
- 2 => 'This is a sample tweet https://example.com/sdfhnjn3',
- 3 => 'This is a sample tweet <a href="https://example.com/sdfhnjn3">https://example.com/sdfhnjn3</a>',
- )
Add Comment
Please, Sign In to add comment