{"id":1735,"date":"2021-01-24T02:22:18","date_gmt":"2021-01-23T20:22:18","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1735"},"modified":"2021-01-24T02:22:24","modified_gmt":"2021-01-23T20:22:24","slug":"regex-to-create-hyperlinks-in-a-string-using-php","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/regex-to-create-hyperlinks-in-a-string-using-php\/","title":{"rendered":"Regex to Create Hyperlinks in a String Using PHP"},"content":{"rendered":"<p>This is a PHP regex script that identifies URLs in a string and converts them into an HTML href tag.<\/p>\n\n\n\n<p>This is handy to let users free-input data as they like and then use logic on the backend to give them the user experience they expect.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1000\" height=\"800\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/01\/blog-post-regex-parse-url.png\" alt=\"\" class=\"wp-image-1736\" title=\"php regex to parse url from string\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/01\/blog-post-regex-parse-url.png 1000w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/01\/blog-post-regex-parse-url-500x400.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/01\/blog-post-regex-parse-url-768x614.png 768w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/01\/blog-post-regex-parse-url-150x120.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/01\/blog-post-regex-parse-url-600x480.png 600w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>This regex could of course be used in Javascript, python, or any other language.&nbsp; I&#8217;m just sharing the concept here &#8211; feel free to adapt and use as needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PHP regex Function to parse URLs<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">function makeLink($string) {\n\t\/\/ look for url in string\n\t$pattern1 = '\/(http\\:|ftp\\:|https\\:)?(\\\/\\\/)?([\\w_-]+(?:(?:\\.[\\w_-]+)+)[\\w.,@?^=%&amp;:\\\/~+#-()].*[\\w@?^=%&amp;\\\/~+#-]?)(?=\\s|$)\/i';\n\t$replacement1 = '&lt;a href=\"${1}${2}${3}\" target=\"_blank\"&gt;${1}${2}${3}&lt;\/a&gt;';\n\t$pass1 = preg_replace( $pattern1 , $replacement1 , $string );\n\n\t\/\/ add a leading https:\/\/ if not present\n\t$pattern2 = '\/href=\"([\\w_-]+(?:(?:\\.[\\w_-]+)+)[\\w.,@?^=%&amp;:\\\/~+#-()]*[\\w@?^=%&amp;\\\/~+#-()])?\"\/';\n\t$replacement2 = 'href=\"https:\/\/${1}\"';\n\t$pass2 = preg_replace( $pattern2 , $replacement2 , $pass1 );\n\n\treturn $pass2;\n}\n\n$findTheUrl = \"This is a string with a URL in it. https:\/\/regex101.com\/r\/1VAnxk\/1\/ Can PHP find it?\";\nmakeLink( $findTheUrl );<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Testing The Regex<\/strong><\/h3>\n\n\n\n<p>You can do a live test of this regex at <a href=\"https:\/\/regex101.com\/r\/1VAnxk\/1\" data-lasso-id=\"661\">regex101.com<\/a>.&nbsp; Feel free to try your own URLs to stress-test the logic here.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is a PHP regex script that identifies URLs in a string and converts them into an HTML [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1736,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[13,5],"tags":[],"class_list":["post-1735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-development","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2021\/01\/blog-post-regex-parse-url.png","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-rZ","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1735"}],"version-history":[{"count":1,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1735\/revisions"}],"predecessor-version":[{"id":1737,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1735\/revisions\/1737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1736"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}