Jump to content

Preg replace in php mysql


PNewCode

Recommended Posts

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>

 

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

@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

Link to comment
Share on other sites

  • Barand changed the title to Preg replace in php mysql

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);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.