Link to home
Start Free TrialLog in
Avatar of dansker69
dansker69

asked on

using 'preg_replace' in php wiki

I am trying to format a wiki hyperlink to a local word document.

The location of the document is for example at:
\\testserver\general\test_House\SCM-Deliver\this_is_a_test.doc

How can I format this using 'preg_replace'. Something similar to the code below

// Prepare page formatting
    elseif ($action <> "edit") {
        $CONTENT = htmlentities($CONTENT);
            $CONTENT = preg_replace("/&amp;#036;/Umsi", "&#036;", $CONTENT);
            $CONTENT = preg_replace('#\[(.+)\|h(ttps?://[0-9a-zA-Z\.\#/~\-_%=\?\&amp;,\+]*)\]#U', '<a href="xx$2">$1</a>', $CONTENT);
            $CONTENT = preg_replace('#h(ttps?://[0-9a-zA-Z\.\&amp;\#\:/~\-_%=?]*\.(jpg|gif|png))#i', '<img src="xx$1" />', $CONTENT);
            $CONTENT = preg_replace('#(https?://[0-9a-zA-Z\.\&amp;\#\:/~\-_%=?]*)#i', '<a href="$0">$1</a>', $CONTENT);
            $CONTENT = preg_replace('#(file?:///[0-9a-zA-Z\.\&amp;\#\:/~\-_%=?]*)#i', '<a href="$0">$1</a>', $CONTENT);
            $CONTENT = preg_replace('#xxttp#', 'http', $CONTENT);
            
The above is used for ordinary http links.

Any suggestions?
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

dansker69,

You won't be able to use that same path in your html and have it resolve.  In other words \\testserver\..... doesn't mean anything on your user's computer in his browser.  In fact browser security would stop it from working even if he had a share called testserver.

What type of tag or element should be used (e.g. anchor, image)?  What is the URL for that DNC path?  If the web user won't have direct access to it then you need to use some server page to get the file.

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of dansker69
dansker69

ASKER

I was looking at something similar to:
$CONTENT = preg_replace("/\@([0-9a-zA-Z\- :\.,\(\)\']+)\@/U", '<a href="file://ftr245/general/In_House/$1">$1</a>', $CONTENT);

Placing a filename between the @ @ gives me a usable link, but only to 'In_House' folder.

I need a variable simliar to $1, which will accept a folder name and a filename
folder = XX file=YY, such as @xx,yy@. This will be used on an intranet.
How well this would work would really depend on the content but a basic expression that will get the folder(s) and name from between @'s is ...

@(.+\\)([^\\]+)@

The first group would be the folders and the second group the file name.  For example with ...

@test_House\SCM-Deliver\this_is_a_test.doc@

... the result is ...

$1 is test_House\SCM-Deliver\
$2 is this_is_a_test.doc

Let me know how this helps or if you have a question.

bol
There's a number of possible ways to do this, here's one:
<?php
$sourcestring='this is a test @testserver\general\test_House\SCM-Deliver\this_is_a_test.doc@ for preg_replace';
echo preg_replace('/@(.*?)@/e','str_replace(chr(92),"/","<a href=\"file://$1\">$1</a>")',$sourcestring);
?>

Open in new window

The solutions up to now are partially correct. To better explain the problem.
Anything between @@@     @@@

@@@Link Text|\\testserver\SCM-Deliver\this_is_a_test.doc@@@
needs to be preg_replaced with
<a href=\"file://testserver\SCM-Deliver\this_is_a_test.doc">Alias Link Text</a>

Does this help clarify the problem?
Consider this code (I escaped the double \ in your source string with \'s so that PHP would consider them two literal slashes instead of one):
<?php
$sourcestring='test @@@Link Text|\\\\testserver\SCM-Deliver\this_is_a_test.doc@@@ test';
echo preg_replace('/@@@([^@]*)\|([^@]*)@@@/se','str_replace(chr(92),"/","<a href=\"file:$2\">$1</a>")',$sourcestring);
?>

Open in new window

The above does not work. The output of anything between the two @@@, should be a hyperlink.
@@@Link Text|\\testserver\SCM-Deliver\this_is_a_test.doc@@@

Where @@@Link Text| is the Link text and
|\\testserver\SCM-Deliver\this_is_a_test.doc@@@ is the link itself.
dansker69, show a specific example that you would like me to follow, your example in post 20947040 would not work because the file:// requires all following slashes to be forward slashes instead of backslashes as in your example.

ref:
http://en.wikipedia.org/wiki/File:_URL

Output of my script from post 20947780:
test <a href="file://testserver/SCM-Deliver/this_is_a_test.doc">Link Text</a> test
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works perfectly.
Thanks for the question and the points.