PNewCode Posted March 14, 2023 Share Posted March 14, 2023 I decided to learn a little bit about preg_replace in strings while I have a couple days of down time from activities away from the computer. I have a table that echo's the results from the mysql value in the column "comments" That works Now I added some code that I had to collect and change a LOT of times to make it work, that shows line breaks, and also converts links in the text to url's that open in a new window. Which is AWESOME! The problem is, I want to have it also work this wizard-like magic for mailto, www, and just something.com For example... What it works on now is any text that is a full written url such as https://www.google.com I also need this to work for, examples (not these specific urls, just whatever is in the text) www.google.com google.com person@gmail.com I came KIND of close, but the ones that were without the https:// in front, just ended up resulting in a link that took them to (for example) https://www.mysite.com/www.google.com So I scrapped all of that because it was making a mess. The code below is what I have that still works with the first part of this post that I have working for a full link I'm excited to see what I learn from this one ///// a bunch of connection to the database stuff here, removed for posting //// $comment = $row["comment"]; $comment = nl2br(preg_replace_callback('~(\bhttps?://\S+)(?:\s+\[([^]]+)\])?~i', function ($m) { $s='<a hr'.'ef="'.$m[1].'" target="_blank">'; $s .= isset($m[2])? $m[2]:$m[1]; return $s.'</a>';}, $comment)); //// echos out below //// <font face="Verdana, Arial, Helvetica, sans-serif" color="#ffffff"><b>'.$comment.'</b></font> Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/ Share on other sites More sharing options...
requinix Posted March 14, 2023 Share Posted March 14, 2023 Take it one step at a time. Do you have a regular expression that can match "www.google.com"? Do you have a regular expression that can match "google.com"? Do you have a regular expression that can match "person@gmail.com"? Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/#findComment-1606516 Share on other sites More sharing options...
PNewCode Posted March 14, 2023 Author Share Posted March 14, 2023 @requinix No I don't have that in there. This is what I tried (sorry I should have put this in the original post) $comment = nl2br(preg_replace_callback('~(\bhttps|http|www|mailto|.?://\S+)(?:\s+\[([^]]+)\])?~i', and $comment = nl2br(preg_replace_callback('~(\bhttps,http,www,mailto,.?://\S+)(?:\s+\[([^]]+)\])?~i', and $comment = nl2br(preg_replace_callback('~(\bhttps?://\S+)(?:\s+\[([^]]+)\])?~i', $comment = nl2br(preg_replace_callback('~(\bhttp?://\S+)(?:\s+\[([^]]+)\])?~i', $comment = nl2br(preg_replace_callback('~(\bwww?://\S+)(?:\s+\[([^]]+)\])?~i', $comment = nl2br(preg_replace_callback('~(\bmailto?://\S+)(?:\s+\[([^]]+)\])?~i', $comment = nl2br(preg_replace_callback('~(\b.?://\S+)(?:\s+\[([^]]+)\])?~i', But none of those worked Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/#findComment-1606517 Share on other sites More sharing options...
requinix Posted March 14, 2023 Share Posted March 14, 2023 Throwing syntax at the function and seeing if it does what you want isn't a very efficient way of programming. Do you understand how those regular expressions work? What the various symbols mean? Again: one step at a time. This ~(\bhttps?://\S+)(?:\s+\[([^]]+)\])?~i is a good enough regular expression that will match these: http://www.google.com https://www.google.com http://www.google.com [Link text here] https://www.google.com [Link text here] Did you know about the [text] bit at the end? Do you want that? 1 Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/#findComment-1606518 Share on other sites More sharing options...
PNewCode Posted March 14, 2023 Author Share Posted March 14, 2023 @requinix You're right. I have a very backwards way of learning stuff. I suspect that stems from making radios and stuff when I was a kid, and just pairing wires (so to speak) that looked like other working radios till it worked, then I would be able to see why those connections worked. This isn't good practice for this. I'll try to change my habbits The text that is for the link is picked up automatically with this, from within the comment. So for example if the user enters I wanted to get my new shoes on sale but they are only discounted at https://www.shoes.com so I went to the website It would echo on the page I wanted to get my new shoes on sale but they are only discounted at https://www.shoes.com so I went to the website with the website as a link OH... and as I'm typing this, it looks like this forum is doing that too haha. Thats awesome. Okay so now I'm more confused than I thought I was. Where do I add the other options in that? Admittingly, the "https" is the only thing in all of that, that is recognizable to me haha Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/#findComment-1606525 Share on other sites More sharing options...
PNewCode Posted March 14, 2023 Author Share Posted March 14, 2023 @Barand Thank you for changing the title for me. I was trying to correct that haha Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/#findComment-1606526 Share on other sites More sharing options...
kicken Posted March 14, 2023 Share Posted March 14, 2023 You should read through the PCRE section of the manual to get an idea of what the various things in the patterns do, and what is available to use when constructing patterns. Using an online regex debugger can help you as well by letting you easily try patterns and break them down into their components. 1 Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/#findComment-1606528 Share on other sites More sharing options...
PNewCode Posted March 14, 2023 Author Share Posted March 14, 2023 Between what @requinix and @kicken posted and gave for references and things to focus on, and what I looked online, this is what I have so far. One pesky thing left is if there is no www or http or https and is, for example google.com side note... once I get this one figured out, I'm going to take on getting the default thumbnails for urls Any thoughts on that last bit? //If it has https// $comment= nl2br(preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $comment)); //if it doesn't have https// $comment= nl2br(preg_replace("#(^|[\n ])((www)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"https://\\2\" target=\"_blank\" rel=\"nofollow\">\\2</a>", $comment)); //Make the email address clickable to mailto stuff// $comment= nl2br(preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $comment)); //To turn the http into https// $comment= str_replace('http://', 'https://', $comment); Quote Link to comment https://forums.phpfreaks.com/topic/316006-preg-replace-in-php-mysql/#findComment-1606529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.