Archive for the ‘PHP’ Category

Find links within text and convert to active links with PHP

July 12th, 2010 5 comments

Recently I have had to integrate some tweets into a clients website via RSS. The problem I found is that the links are hidden in the description and are not active. The solution is as simple as using 3 regular expressions and replace the text URL with a HTML URL using PHPs eregi_replace function.

  3. function activateLinks($string)
  4. { 
  6. // This regular expression looks for <strong>http:// </strong>type url
  7. $string = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_+.~#?&//=]+)',
  8. '<a href="1" target=_blank>1</a>', $string);
  10. // This regular expression looks for <strong>www. </strong>type url
  11. $string = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)',
  12. '1<a href="http://2" target=_blank>2</a>', $string);
  14. // This regular expression looks for <strong>[email protected]</strong>
  15. $string = eregi_replace('([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})',
  16. '<a href="mailto:1" target=_blank>1</a>', $string);
  18. return $string;
  20. }
  22. //call function where ever you need it
  23. $string= 'Scaling a dynamic background image in proportion using flashvisit
  24. ( or email me on [email protected]';
  26. echo activateLinks($string);

The output of the string would be

  2. //output of $string
  3. $string = 'Scaling a dynamic background image in proportion using flash visit
  4. (<a href=""></a>) or
  5. email me on <a href="mailto:[email protected]"> [email protected]</a>';

My next post will be how to read your twitter posts with php and then use this function to active the links within them.

Happy coding...

List files in a directory using php

September 13th, 2009 1 comment

To display a list of files within a directory using PHP you need to use opendir() and readdir(). Below is a function that will return the list of files and remove the . and .. from the listing.

  2. //full path of directory
  3. $dir = "/var/www/images/";
  5. function listFiles($dir){
  7. //open directory and read its contents
  8. if ($handle = opendir($dir)) {
  9. while (false !== ($file = readdir($handle))) {
  11. //only display filenames
  12. if ($file != "." && $file != "..") {
  13. echo "$file\n";
  14. }
  15. }
  16. closedir($handle);
  17. }
  18. }

You might run into trouble trying to find the correct full path of the images. For example in a windows hosting environment the path could be  (c://blah/blah), in a Linux environment it could be (/var/www/blah/blah).

To find the exact path for your environment use phpinfo(). Once you have the php info look for the DOCUMENT_ROOT entry.

Dynamic variable names in PHP

August 18th, 2009 No comments

I'm going to show you how to use dynamic variable names in php. It's quite simple, just put your variable name in between {} symbols.

  1. // declare prefix. In a loop this would probably be a number.
  2. $prefix = "pfx";
  4. // build your variable
  5. ${"varname_{$prefix}"} = "success";
  7. //This would output "success"
  8. echo $varname_pfx;

Simple isn't it....

Become a dyneaming online video poker game player on the video poker games of where you can also learn the history of video poker !

Images not working in dompdf

August 6th, 2009 24 comments

What is dompdf?  Well basically its a HTML to PDF converter. It's rendering engine is built in PHP and is style-driven which means it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements.

I won't go into detail of how to use it (unless you want me to or would like to write about it), but recently I  had an issue with images not rendering  in the pdf. At first I thought it was a permissions problem, then I thought it was an image type problem. It turns out it was a file location issue.

instead of the file location being:

<img src="images/myimage.jpg" style="width:200px;height:200px">

It should be:

<img src="/var/www/images/myimage.jpg" style="width:200px;height:200px">

So as you can see it needs to reference the file from the server side file directory and not just from within the web directory.

Wish someone wrote this, when I needed it....

SEO Powered by Platinum SEO from Techblissonline