Hey guys...
I'm trying to get the following BBcode text

[file]C:\Documents and Settings\Administrator\Desktop\WS_FTP.LOG[/file]

to change in to a URL to the document....
here's what I have - but it doesnt seem to work

$str = preg_replace("/\[file\]( a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\')\[\/file\]/", '<a href="$1" target="_blank">$1</a>', $str);

where am I going wrong?
thanks

    use this instead

    $str = preg_replace("/[file]([a-zA-Z0-9:\/-?&.=~#']+)[\/file]/", '<a href="$1" target="blank">$1</a>', $str);

    your code went wrong because []/ are all special chars with meanings in them therefore u have to escape them using . You also have to put + to indicate that a-zA-Z0-9:\/-?&.=_~#' chars can appear more than once..
    so this should be it..

      soori
      forgot 1 thing u must oso add a backslash before the dot

      this is the corrected version

      $str = preg_replace("/[file]([a-zA-Z0-9:\/-?&.=~#']+)[\/file]/", '<a href="$1" target="blank">$1</a>', $str);

        cheers for the reply... however, it doesnt seem to work!

        $str = "[file]C:\Documents and Settings\Administrator\Desktop\WS_FTP.LOG[/file]";
        
        $str = preg_replace("/\[file\]([a-zA-Z0-9:\/-?&\.=_~#']+)\[\/file\]/", '<a href="$1" target="_blank">$1</a>', $str);
        
        

        is it something to do with the slashes in the file path?

          It works. I tried it at home.
          U didn't add the backslash before the [,] characters like i did in the post??

          [file] // these backslashes are important and cannot be missed out.

            sorry - the PHP

             above seems to have stripped out the slashes... they were there....
            
            here's the code which works
            
            $str = "[file]hello[/file]";
            $str = preg_replace("/\[file\]([a-zA-Z0-9:\/-?&\.=_~#']+)\[\/file\]/", '<a href="$1" target="_blank">$1</a>', $str);
            nl2br($str);
            
            that's ok - the "hello" is turned in to a hyperlink - perfect... but when I do
            
            $str = "[file]C:\Documents and Settings\Administrator\Desktop\WS_FTP.LOG[/file]";
            
            the above isnt converted....
            so it looks like it's the path?

              I don't see the backslash as allowed character within the character class. You would probably have to add \\ to match it literally.

                Not sure if I get your question, but I think it is likely for any applications that offers bb-style formatting options, the parsing is done either with str_replace (suitable only for simple tags like -> <b>) or regex. the pattern however may differ from yours, and differ between applications 😉

                  bbcode can use str_replace for things like etc... but I am fairly sure they use preg_replace for things like emails, and web addresses.

                  I'm 100% it's a problem with the back slashes..... I just cant get rid of it!!

                    Originally posted by mfacer
                    bbcode can use str_replace for things like etc... but I am fairly sure they use preg_replace for things like emails, and web addresses.


                    Yes, this is what I was trying to say.


                    I'm 100% it's a problem with the back slashes..... I just cant get rid of it!!


                    Is the problem still persitent? Did you try to include \\ in the pattern? Other options may be to be less restrictive and e.g. use \S+ (sequence of non-whitespace characters) as definition for allowed characters.

                      woo! got it to work.... added the \\ and it still didnt.... but then noticed there was no space in the conditions....!!

                      so this worked

                      $str = preg_replace("/[file]([a-zA-Z0-9:\/-?&\\.=~# ']+)[\/file]/", '<a href="file://$1" target="blank">$1</a>', $str);

                      the problem is that when you click the hyperlink, nothing happens! It's meant to open in a new window.... do I need to url-encode it or something? is that possible within the preg_replace function?? thanks

                        your $str = "[file]C:\Documents and Settings\Administrator\Desktop\WS_FTP.LOG[/file]"; doesn;t work b'cuz it contains spaces. Your original code did not try to match spaces so it would work.

                        use this instead. I believe it should work now

                        $str = preg_replace("/[file]([a-zA-Z0-9:\/-?&\\.=~#'\s]+)[\/file]/", '<a href="$1" target="blank">$1</a>', $str);

                        The hyperlink to the C:... can only work if the file is found on the local file system not on the server.

                          its all working now thanks - when I link to a file on the network - it doesnt work - nor does it work with local files.... when I put them in the browser it opens the files ok - but not through the hyperlink.....

                          eg: the hyperlink when you look at the source code is

                          file:///D:\My Documents\AUCTIONS\camera2.jpg

                          but when you use "copy link location" in the browser menu, it looks like this

                          file:///D:%5CMy%20Documents%5CAUCTIONS%5Ccamera2.jpg

                          and that above works no problem... so when you add the %20 for space and %5 for backslash - it's ok.

                          any ideas?? thanks for all the help 🙂

                            Write a Reply...