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\th is_is_a_te st.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("/&#036;/ Umsi", "$", $CONTENT);
$CONTENT = preg_replace('#\[(.+)\|h(t tps?://[0- 9a-zA-Z\.\ #/~\-_%=\? \&,\+] *)\]#U', '<a href="xx$2">$1</a>', $CONTENT);
$CONTENT = preg_replace('#h(ttps?://[ 0-9a-zA-Z\ .\&\#\ :/~\-_%=?] *\.(jpg|gi f|png))#i' , '<img src="xx$1" />', $CONTENT);
$CONTENT = preg_replace('#(https?://[ 0-9a-zA-Z\ .\&\#\ :/~\-_%=?] *)#i', '<a href="$0">$1</a>', $CONTENT);
$CONTENT = preg_replace('#(file?:///[ 0-9a-zA-Z\ .\&\#\ :/~\-_%=?] *)#i', '<a href="$0">$1</a>', $CONTENT);
$CONTENT = preg_replace('#xxttp#', 'http', $CONTENT);
The above is used for ordinary http links.
Any suggestions?
The location of the document is for example at:
\\testserver\general\test_
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("/&#036;/
$CONTENT = preg_replace('#\[(.+)\|h(t
$CONTENT = preg_replace('#h(ttps?://[
$CONTENT = preg_replace('#(https?://[
$CONTENT = preg_replace('#(file?:///[
$CONTENT = preg_replace('#xxttp#', 'http', $CONTENT);
The above is used for ordinary http links.
Any suggestions?
ASKER
I was looking at something similar to:
$CONTENT = preg_replace("/\@([0-9a-zA -Z\- :\.,\(\)\']+)\@/U", '<a href="file://ftr245/genera l/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.
$CONTENT = preg_replace("/\@([0-9a-zA
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\th is_is_a_te st.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
@(.+\\)([^\\]+)@
The first group would be the folders and the second group the file name. For example with ...
@test_House\SCM-Deliver\th
... 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);
?>
ASKER
The solutions up to now are partially correct. To better explain the problem.
Anything between @@@ @@@
@@@Link Text|\\testserver\SCM-Deli ver\this_i s_a_test.d oc@@@
needs to be preg_replaced with
<a href=\"file://testserver\S CM-Deliver \this_is_a _test.doc" >Alias Link Text</a>
Does this help clarify the problem?
Anything between @@@ @@@
@@@Link Text|\\testserver\SCM-Deli
needs to be preg_replaced with
<a href=\"file://testserver\S
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);
?>
ASKER
The above does not work. The output of anything between the two @@@, should be a hyperlink.
@@@Link Text|\\testserver\SCM-Deli ver\this_i s_a_test.d oc@@@
Where @@@Link Text| is the Link text and
|\\testserver\SCM-Deliver\ this_is_a_ test.doc@@ @ is the link itself.
@@@Link Text|\\testserver\SCM-Deli
Where @@@Link Text| is the Link text and
|\\testserver\SCM-Deliver\
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/SC M-Deliver/ this_is_a_ test.doc"> Link Text</a> test
ref:
http://en.wikipedia.org/wiki/File:_URL
Output of my script from post 20947780:
test <a href="file://testserver/SC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This works perfectly.
Thanks for the question and the points.
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