?
Solved

using 'preg_replace' in php wiki

Posted on 2008-02-04
11
Medium Priority
?
823 Views
Last Modified: 2013-12-13
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?
0
Comment
Question by:dansker69
  • 5
  • 4
  • 2
11 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20819463
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
0
 

Author Comment

by:dansker69
ID: 20832704
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.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20847695
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
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 27

Expert Comment

by:ddrudik
ID: 20863323
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

0
 

Author Comment

by:dansker69
ID: 20947040
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?
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20947780
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

0
 

Author Comment

by:dansker69
ID: 20948197
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.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20948234
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
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 20948253
From the wiki page it noted that file:// URL's that use network shares should be in the format of file:////server/file:
<?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);
?>
Does that work?
0
 

Author Comment

by:dansker69
ID: 20948315
This works perfectly.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20948330
Thanks for the question and the points.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question