473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Simple Regex help, need a minor modification to this

jamwil
13
What up friends, I'm using this little function to parse hyperlinks in plain text and convert them into links. It goes like so
Expand|Select|Wrap|Line Numbers
  1. private function hyperlink($text) {
  2.         // match protocol://address/path/
  3.         $text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text);
  4.  
  5.         // match www.something
  6.         $text = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);
  7.  
  8.         // match @replys
  9.         $text = preg_replace("/[@]+[A-Za-z0-9-_]+/", "<a href=\"http://twitter.com/\\0\">\\0</a>", $text);
  10.  
  11.         // match #hashtags
  12.          $text = preg_replace("/[#]+[A-Za-z0-9-_]+/", "<a href=\"http://search.twitter.com/search?q=\\0\">\\0</a>", $text);
  13.  
  14.         // fix Twitter URLs
  15.         $text= str_replace("<a href=\"http://twitter.com/@","<a target=\"_blank\" href=\"http://twitter.com/",$text);
  16.         $text= str_replace("<a href=\"http://search.twitter.com/search?q=#","<a target=\"_blank\" class=\"hashtag\" href=\"http://search.twitter.com/search?q=%23",$text);
  17.  
  18.         // return $text
  19.         return $text;
  20.     }
...This works great however it doesn't parse URL's that have query strings at the end such as
http://example.com/blah?question=answer
. If somebody could modify it to make it work with those I would be extremely grateful. Also, if it's not to difficult, it would be extremely beneficial to have it parse blip urls in the form
http://blip.com/~7cwjh
Thanks a bunch guys!
May 31 '09 #1
4 1580
Atli
5,058 Expert 4TB
Hi.

Check out example #3 in the manual entry for ereg_replace.
Should work for both of your strings.
May 31 '09 #2
jamwil
13
Cool I'll give that a shot.. I had originally gone there to find the solution, and ended up using the one that is posted 2 down from there as the guy claims the original code was wrong.

I'll try out that code and see how she goes.

Thanks a bunch!
May 31 '09 #3
jamwil
13
Works like a charm... Thanks!
Jun 1 '09 #4
Atli
5,058 Expert 4TB
The PHP manual entries are pretty much always right.
The comments, however, aren't always as dependable.

Anyways, I'm glad you've found a solution :)
Jun 1 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
3
by: Vidar Skjelanger | last post by:
I have a regex for matching VB6-functions, but it hangs on one specific function. The regex: ...
7
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
4
by: momo | last post by:
Hello all, I need help with this. I have a datagrid (dg1) with checkboxes and I want when some rows are selected to populate another datagrid (dg2) with the selected rows from (dg1). Thanks ...
6
by: Allan Rojas | last post by:
Hi there, i'm having this problem and i hope someone can help me out. We have this WebApplication in ASP.NET; we work on it in our local webserver. When we're done, we must upload the application...
2
by: K. Shier | last post by:
my regex for matching phone #'s: \({0,1}(?<areacode>{3})\){0,1}( |-){0,1}(?<prefix>{3})( |-){0,1}(?<suffix>{4}) which i then .Replace to become: (${areacode}) ${prefix}-${suffix} question: ...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
7
by: Aek | last post by:
Hi everyone, I am trying to construct a regular expression and format string to use with a boost::regex_replace() In my file the sample text is: // .fx shader file FLOAT JOE 3545f; FLOAT...
1
by: anglaissam | last post by:
I have a regex that is designed to help improve readability for a html document. "(?=((?!<\/?em).)*<\/em>) The purpose of this regex is to escape " marks from within <EM> affected sentences....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.