Aretai Posted March 18, 2007 Share Posted March 18, 2007 Hi, I've a problem with regex and PHP. I'm trying to link to a files stored on my server (Solaris) with grep command: grep -irU $input /opt/users/aretai $input is the text that is being searched by the user. Then it's just simple grep command (recursive and binary files are matched). While this command is being run on my server it produces the list like this: Binary file /opt/users/aretai/projects/test/test.doc matches Binary file /opt/users/aretai/projects/fun/games.xls matches Binary file /opt/users/aretai/projects/home_tools/tools.doc matches etc /opt/users/aretai/projects/test.txt:search last line is for the file created with vi under linux 'search' is the string that was being searched this time. I want to create a link to a given file and then pull it on the website: the output of the command is stored in the variable $output: $output = 'grep -irU $input /opt/users/aretai'; I want to replace the text "Binary file /opt/users/aretai/projects" and "/opt/users/aretai/projects/" with a <link> tag and " matches" or :$input with a </link> tag then it will be displayed as a link on my page. So far I've achieved that I get a list of the files, but there is a little bit more I get a link of the whole output instead of a single line. thx for answers, Aretai Quote Link to comment Share on other sites More sharing options...
effigy Posted March 19, 2007 Share Posted March 19, 2007 <pre> <?php $string = 'Binary file /opt/users/aretai/projects'; echo preg_replace('/^(?:Binary file )?(\S+)/', '<a href="\1">\1</a>', $string); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.