Re: [PHP] Converting URL's to hyperlinks.

2009-09-08 Thread Paul M Foster
On Wed, Sep 09, 2009 at 03:22:25AM +0200, Lupus Michaelis wrote:

>   Sorry for the lag.
>
> Daevid Vincent a écrit :
>>>OP ?
>>
>> Original Poster
>
>   Thanks :)
>
>> Blah blah blah.
>> I've used this code for about 6 years now and have yet to find emails that
>> it didn't work for. If someone has some funky (whacky) RFC extremity, then
>> so be it. That's their problem. Most people have NORMAL emails that follow
>> the above.
>   I faced a professionnal issue because of a lazy programmer (a Delphi
> component that was not aware about "Toto man"@example.com). What you
> consider normal e'mail is a subset that is not interoperable. And I hate
> that.
>
>> But you are correct, I have revised it to be a little more forgiving
> of some
>> allowed characters...
>>
>> preg_replace("/([\w\.\-...@[\w\.\-_]+\.\w{2,6})/i",
>   You can revised your regex to fit to the new kind of email. But it is
> smarter to use the right tool (like filter_vars).
>
>   Yeah, I know, I feel like some spanish windmill hunter.

I'm not sure it's well known on this list, but one resource I use for
regexps is:

http://www.regexlib.com/

Paul

-- 
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-09-08 Thread Lupus Michaelis

  Sorry for the lag.

Daevid Vincent a écrit :

   OP ?


Original Poster


  Thanks :)


Blah blah blah.
I've used this code for about 6 years now and have yet to find emails that
it didn't work for. If someone has some funky (whacky) RFC extremity, then
so be it. That's their problem. Most people have NORMAL emails that follow
the above.
  I faced a professionnal issue because of a lazy programmer (a Delphi 
component that was not aware about "Toto man"@example.com). What you 
consider normal e'mail is a subset that is not interoperable. And I hate 
that.



But you are correct, I have revised it to be a little more forgiving of some
allowed characters...

preg_replace("/([\w\.\-...@[\w\.\-_]+\.\w{2,6})/i",
  You can revised your regex to fit to the new kind of email. But it is 
smarter to use the right tool (like filter_vars).


  Yeah, I know, I feel like some spanish windmill hunter.

--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-09-04 Thread Tom Chubb
2009/9/4 Daevid Vincent 

>
>
> > -Original Message-
> > From: Lupus Michaelis 
> > [mailto:mickael+...@lupusmic.org
> ]
> > Sent: Friday, September 04, 2009 7:46 AM
> > To: php-general@lists.php.net
> > Subject: Re: [PHP] Converting URL's to hyperlinks.
> >
> > Daevid Vincent a écrit :
> > > Maybe I misunderstood the OP,
> >OP ?
>
> Original Poster
>
> > > but wouldn't this (or something like it) be
>
> Note the "something like it". I didn't write his app, just provided a
> starting point.
>
> > > $url = preg_replace("/(\...@\w+\.[a-za-z]{2,3})/i", " > > href='mailto:$1'>$1", $url);
> >This violate the numerous RFC about mail addresses, and some other
> > stuffs.
>
> Blah blah blah.
> I've used this code for about 6 years now and have yet to find emails that
> it didn't work for. If someone has some funky (whacky) RFC extremity, then
> so be it. That's their problem. Most people have NORMAL emails that follow
> the above.
>
> But you are correct, I have revised it to be a little more forgiving of
> some
> allowed characters...
>
> preg_replace("/([\w\.\-...@[\w\.\-_]+\.\w{2,6})/i",
>
> > >
> > > $url =
> > preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s)/i", "  > > href='http://$2' target='_blank'>$2", $url);
> >
> >Same as previously. What about .info, .museum and so on tld ?
>
> What about them? It's going to depend on how/where you use this. In my
> case,
> 2 and 3 letter domains are all I encounter. It's trivial to make it {2,6}
> if
> you really are going to encounter a .museum domain. DOUBTFUL, but sure,
> I'll
> concede the three extra letters. ;-)
>
> However, you point out a few edge cases and so I optimized mine and now use
> this one:
> http://snipplr.com/view/2371/regex-regular-expression-to-match-a-url/
>
> > > Oh, and your regex isn't
> > smart (you use the case insensitivity flag, but seek A-Z
> > characters...) :D
>
> Noted. Thanks for the optimization.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Thanks for posting that! Will be really handy for me!


RE: [PHP] Converting URL's to hyperlinks.

2009-09-04 Thread Daevid Vincent
 

> -Original Message-
> From: Lupus Michaelis [mailto:mickael+...@lupusmic.org] 
> Sent: Friday, September 04, 2009 7:46 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Converting URL's to hyperlinks.
> 
> Daevid Vincent a écrit :
> > Maybe I misunderstood the OP,
>OP ?

Original Poster

> > but wouldn't this (or something like it) be

Note the "something like it". I didn't write his app, just provided a
starting point.

> > $url = preg_replace("/(\...@\w+\.[a-za-z]{2,3})/i", " > href='mailto:$1'>$1", $url);
>This violate the numerous RFC about mail addresses, and some other 
> stuffs.

Blah blah blah.
I've used this code for about 6 years now and have yet to find emails that
it didn't work for. If someone has some funky (whacky) RFC extremity, then
so be it. That's their problem. Most people have NORMAL emails that follow
the above.

But you are correct, I have revised it to be a little more forgiving of some
allowed characters...

preg_replace("/([\w\.\-...@[\w\.\-_]+\.\w{2,6})/i",

> > 
> > $url = 
> preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s)/i", "  > href='http://$2' target='_blank'>$2", $url);
> 
>Same as previously. What about .info, .museum and so on tld ? 

What about them? It's going to depend on how/where you use this. In my case,
2 and 3 letter domains are all I encounter. It's trivial to make it {2,6} if
you really are going to encounter a .museum domain. DOUBTFUL, but sure, I'll
concede the three extra letters. ;-)

However, you point out a few edge cases and so I optimized mine and now use
this one:
http://snipplr.com/view/2371/regex-regular-expression-to-match-a-url/

> > Oh, and your regex isn't 
> smart (you use the case insensitivity flag, but seek A-Z 
> characters...) :D

Noted. Thanks for the optimization.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-09-04 Thread Lupus Michaelis

Daevid Vincent a écrit :

Maybe I misunderstood the OP,

  OP ?


but wouldn't this (or something like it) be
easier and cleaner than that mess below??

  No, it's dirty too.


$url = preg_replace("/(\...@\w+\.[a-za-z]{2,3})/i", "$1", $url);
  This violate the numerous RFC about mail addresses, and some other 
stuffs.




$url = preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s)/i", " $2", $url);


  Same as previously. What about .info, .museum and so on tld ? The 
filter_var is well suited for this kind of job. Oh, and your regex isn't 
smart (you use the case insensitivity flag, but seek A-Z characters...) :D


--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Converting URL's to hyperlinks.

2009-09-02 Thread Daevid Vincent
Maybe I misunderstood the OP, but wouldn't this (or something like it) be
easier and cleaner than that mess below??

$url = preg_replace("/(\...@\w+\.[a-za-z]{2,3})/i", "$1", $url);

$url = preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s)/i", " $2", $url);


> -Original Message-
> From: Nisse Engström [mailto:news.nospam.0ixbt...@luden.se] 
> Sent: Saturday, August 29, 2009 1:46 PM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Converting URL's to hyperlinks.
> 
> On Fri, 28 Aug 2009 17:22:20 -0600, LinuxManMikeC wrote:
> 
> > click here
> 
> *Groan*
> 
> Throw any random web site to an HTML validator
> and you're likely to see this kind of slop all
> over.
> 
> The correct solution is of course:
> 
>   $u = htmlspecialchars ($url);
>   echo "$u";
> 
> 
> [A more elaborate way to flay this feline is
>  included below.]
> 
> 
> /Nisse
> 
> 
> /* Reworked from slightly different code.
>Bugs may have been introduced. */
> 
>  
>   function url_to_links ($url)
>   {
> if (preg_match ('@^([a-z]+://)(.*)@i', $url, $m)) {
>   $prfx = $m[1];
>   $path = $m[2];
> } else {
>   return htmlspecialchars ($url);
> }
> 
> $url_sofar = $prfx;
> $links = htmlspecialchars ($prfx);
> 
> $segs = explode ('?', $path, 2);
> if (isset ($segs[1]))
>   $query = $segs[1];
> $segs = explode ('/', $segs[0]);
> 
> for ($segn = 0; $segn < count ($segs); $segn++) {
>   $url_sofar .= $segs[$segn];
>   if (isset ($segs[$segn+1]))
> $url_sofar .= '/';
> 
>   if ($segs[$segn] !== '') {
> $links .= ''
>. htmlspecialchars ($segs[$segn]) . '';
>   }
> 
>   if (isset ($segs[$segn+1]))
> $links .= '/';
> }
> 
> if (isset ($query)) {
>   $url_sofar .= "?$query";
>   $links .= '?' . htmlspecialchars ($query) . '';
> }
> 
> return $links;
>   }
> 
>   $u = 'https://ebagwa.example/abd/def/ghi?s=t&u=v&w=x&y=z';
>   $u_h = htmlspecialchars ($u);
>   $links = url_to_links ($u);
> 
>   header ('Content-Type: text/html');
> 
>   echo <<<_
>"http://www.w3.org/TR/html4/strict.dtd";>
> url_to_links()
> 
> 
> $u_h
>   ↓
> $links
> 
> 
> _;
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-08-29 Thread Nisse Engström
On Sat, 29 Aug 2009 16:47:47 -0600, LinuxManMikeC wrote:

> 2009/8/29 Nisse Engström :
>> On Sat, 29 Aug 2009 16:19:05 -0600, LinuxManMikeC wrote:
>>
>>> As for your "more elaborate example", I'm sure that heredoc will
>>> validate nicely.
>>
>> It does.
>>
> 
> Perhaps you haven't met a few good friends of mine.  Their names are
> html, head, and body.

The html, head and body elements are all there. They are
mandatory. The tags however, are optional.


/Nisse

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-08-29 Thread LinuxManMikeC
2009/8/29 Nisse Engström :
> On Sat, 29 Aug 2009 16:19:05 -0600, LinuxManMikeC wrote:
>
>> As for your "more elaborate example", I'm sure that heredoc will
>> validate nicely.
>
> It does.
>

Perhaps you haven't met a few good friends of mine.  Their names are
html, head, and body.  So what crawled up your backside while you were
reading my example?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-08-29 Thread Nisse Engström
On Sat, 29 Aug 2009 16:19:05 -0600, LinuxManMikeC wrote:

> As for your "more elaborate example", I'm sure that heredoc will
> validate nicely.

It does.

> and rethink your code so you aren't processing the same data over and
> over again.  I "see this kind of slop all over."

Touché!

Would you believe that's on my todo list?
Thanks for the reminder. :-)


/Nisse

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-08-29 Thread LinuxManMikeC
2009/8/29 Nisse Engström :
> On Fri, 28 Aug 2009 17:22:20 -0600, LinuxManMikeC wrote:
>
>> click here
>
> *Groan*
>
> Throw any random web site to an HTML validator
> and you're likely to see this kind of slop all
> over.
>
> The correct solution is of course:
>
>  $u = htmlspecialchars ($url);
>  echo "$u";
>
>

Right... you do realize that you validate the HTML output of the
executed PHP script, not the PHP script itself.  All you really did
was just show another way to skin the same cat.  Get over yourself.
As for your "more elaborate example", I'm sure that heredoc will
validate nicely.  It also wouldn't hurt to read a book on algorithms
and rethink your code so you aren't processing the same data over and
over again.  I "see this kind of slop all over."

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-08-29 Thread Nisse Engström
On Fri, 28 Aug 2009 17:22:20 -0600, LinuxManMikeC wrote:

> click here

*Groan*

Throw any random web site to an HTML validator
and you're likely to see this kind of slop all
over.

The correct solution is of course:

  $u = htmlspecialchars ($url);
  echo "$u";


[A more elaborate way to flay this feline is
 included below.]


/Nisse


/* Reworked from slightly different code.
   Bugs may have been introduced. */

'
   . htmlspecialchars ($segs[$segn]) . '';
  }

  if (isset ($segs[$segn+1]))
$links .= '/';
}

if (isset ($query)) {
  $url_sofar .= "?$query";
  $links .= '?' . htmlspecialchars ($query) . '';
}

return $links;
  }

  $u = 'https://ebagwa.example/abd/def/ghi?s=t&u=v&w=x&y=z';
  $u_h = htmlspecialchars ($u);
  $links = url_to_links ($u);

  header ('Content-Type: text/html');

  echo <<<_
http://www.w3.org/TR/html4/strict.dtd";>
url_to_links()


$u_h
  ↓
$links


_;

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Converting URL's to hyperlinks.

2009-08-29 Thread Eric

- Original Message - 
From: "John Meyer" 
To: 
Sent: Friday, August 28, 2009 1:56 AM
Subject: [PHP] Converting URL's to hyperlinks.


> What sort of function would I need if I wanted to convert those URLs 
> from plain jane text?
> 

You should encode the url before printing as usual way

$url = htmlentities('http://www.mysite.com/index.php?act=1&t=10');

echo "mysite";


- Eric

> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
>

Re: [PHP] Converting URL's to hyperlinks.

2009-08-28 Thread LinuxManMikeC
If you have short php tags enabled, assuming you put the url in a
variable called $url...

click here

otherwise...

click here

or a myriad of other ways...

Depending if there are any special characters involved, there are a
couple functions that would be useful for preparing the url before
outputing it in html, but I don't remember them off the top of my
head.  RTFM for that.

On Thu, Aug 27, 2009 at 11:56 AM, John
Meyer wrote:
> What sort of function would I need if I wanted to convert those URLs from
> plain jane text?
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php