php - preg_replace(), and the abbreviations that go with it? -


I have recently been working with PHP for a simple Twitter API and linking the user to Twitter I came to this code for. .com / username Unfortunately, this code makes it so that the @username name, resulting in error. I do not understand many parts of this code, like ^ \ v and others (seen below). Does anyone explain me about them?

  $ ret = preg_replace ('/ (^ | [^ \ w]) (@ [\ d \ w \ -] +) /', '\\ 1 & lt; a href = "Http://twitter.com/$2"> $ 2 & lt; / a & gt; ', $ ret;);  

Easy one:

  $ ret = preg_replace ('/ (^ | [^ \ W]) @ ([\ d \ w \ -] +) /', '\\ 1 & lt; a href = "http://twitter.com/$2" & gt; ; @ $ 2 & lt; / a & gt; ', $ ret;);  

The only difference is that @ was removed from the capturing group, which means that it has to be manually added to the output in the link.

In addition, \ W contains numbers, so \ d is unnecessary. You can do it easily:

  $ ret = preg_replace ('/ ^ | ([^ \ w]) @ ([\ w \ -] +) /', '\ \ 1 & lt; A href = "http://twitter.com/$2" & gt; $ 2 & lt; / a & gt; ', $ ret;);  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -