Page 1 of 1

preg a simple solution?

Posted: Fri Apr 16, 2004 11:24 am
by redmonkey
I'm sure most of us have been here before, i.e. we get too tied up looking for the complex solution and completely miss the simple aproach.

I can't help feeling I'm missing the more simplistict approach with this one...

The problem is to take a url from a text string and convert it to a clickable link, at the same time truncate it's length if it is over a certain length. i.e. those long links people post to ebay auctions.

So I spent about an hour on this but I'm getting the feeling I'm missing something. Heres the code.....

Code: Select all

<?php

function url2link($text)
{
	$html = " ".$text;

  $html = preg_replace_callback("/\b([a-z]+:\/\/(?:[-\w]{2,}\.)*[-\w]{2,}(?::\d+)?(?:(?:[^\s;,.?"'[\](){}<>]|\S[^\s;,.?"'[\](){}<>])*)?)/i",
        create_function('$match', 'return short_url($match[1], 7);'), $html);
	$html = preg_replace_callback("/([^\/])((?:www|ftp)\.(?:[-\w]{2,}\.)+[-\w]{2,}(?::\d+)?(?:(?:[^\s;,.?"'[\](){}<>]|\S[^\s;,.?"'[\](){}<>])*)?)/i",
        create_function('$match', 'return short_url($match[2], 0,$match[1], "http://");'), $html);

    return substr($html, 1);
}

function short_url($url, $offset = 0, $prepend = '', $scheme = '')
{
  $truncated = $url;
  if (strlen($url) > 40 + $offset)
  {
    $truncated = substr($url, 0, 20 + $offset) . '...' . substr($url, -20);
  }
  return $prepend . '<a href="' . $scheme . $url . '">' . $truncated . '</a>';
}

$test_text = 'Hey check this deal on ebay http://cgi.ebay.com/ebaymotors/ws/eBayI ... 106&rd=1';

echo url2link($test_text);

?>
Which at present is working OK it will return......

Code: Select all

Hey check this deal on ebay &lt;a href="http://cgi.ebay.com/ebaymotors/ws/eBayISAPI.dll?ViewItem&amp;category=50019&amp;item=2473512106&amp;rd=1"&gt;http://cgi.ebay.com/ebaymot...item=2473512106&amp;rd=1&lt;/a&gt;
You can see the actual visible link part is truncated. It is still work in progress and the actual regex I'm sure I can refine somewhat, but is there a more simplistict approach. I'm not looking so much for code examples more general ideas.

Any thoughts?

Posted: Fri Apr 16, 2004 11:41 am
by kendall
remonkey,

Aye yo man...i was trying to do the same thing man.... i think i posted a ereg expression that would have changed text strings with urls or emails into links...or was it to remove them..o well do a search for ereg_expressions or something of the sort with my name you should find my postings with it

Kendall

Posted: Fri Apr 16, 2004 11:56 am
by redmonkey
Thanks,

The only thing I found was this....

Code: Select all

<?php
 $EmailSplit = explode("@",$Value);
$emailDomain = $EmailSplit[1];
$pattern = "^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$";
if (!ereg($pattern,$Value)&& !getmxrr($emailDomain,$mxhostsarr))
$ErrorMSG[$Field] = "Please ENTER a VALID ".$Field." Address!"; 
?>
Unfortunately, it is not what I'm looking for.

Posted: Fri Apr 16, 2004 1:34 pm
by kendall
redmonkey,

Code: Select all

$exp = "`((([\w\-\.]+)*://)|([\w\-\.]+@)|(www\.))(([\.\w\-]+)([\w\-\.]+[\w/\.\?\=\&]*))`i";
could have sworn i had this up on the board somewhere but any way i did this to match url's and emails from a text string.

KEndall

Posted: Fri Apr 16, 2004 5:37 pm
by redmonkey
Kendall,

Please don't take offense when I say 'I think you are missing the point'. The regex is not the problem (although it is still subject to change, and any comments on it are welcome). I was merely curious if there wasn't a more simplistic approach, as I said previously I am getting the feeling that I have completly missed the simple answer and headed straight for the complex solution.

I had a quick play with your regex and found it quite difficult to grep out the results, I also found that if the url was '.http://www.somedomain.com' then it was not possible to grep out the http part without including the leading '.'. Also something like http://www.somedomain.com:8080 does not get through as the port number is disregarded.

I realise that my intial regex is not perfect (it is a compromise at present) I'm just trying to work out how far to take it.

Posted: Sat Apr 17, 2004 12:40 pm
by kendall
redmonkey,

i alomst took offense! lucky im not the hulk...lol

well ok...strange that the expression wasn't picking it up...it seem to work for me..no harm no foul

Kendall