douxing5199 2012-02-02 14:14
浏览 49
已采纳

PHP Exec()使用sed给出了奇怪的结果

I have a list of full file paths. All of the full file paths look like "/dir1/dir2/dir3/s..". I want to completely remove the s. from the filename. There is the possibility of a filename being plural, for example s.asdfs.cpp. I do not want to remove the second occurence of s. since that is part of the actual filename and not a reoccuring theme in every full file path in the list.

Running the following in shell works as I want it to:

echo /dir1/dir2/dir3/s.filenames.cpp | sed 's#\(.*\)\/s\.\([^\/]*\)#\1\/\2#g'

Gives the desired result of:

/dir1/dir2/dir3/filenames.cpp

But if I run the following in php:

$formatted_filename = exec("echo ".$filename." | sed 's#\(.*\)\/s\.\([^\/]*\)#\1\/\2#g'");

where

$filename = /dir1/dir2/dir3/s.filenames.cpp;

And then in my bash shell run

php -q script_name_that_contains_command_above.php > test.html

and refresh my firefox browser that displays test.html I get very strange results. In place of where this edited file path should be listed I get

<strange box>/<strange box> 

where

<strange box> 

is a small box with 2 rows and 2 columns consisting of 0's except for the bottom right cell. The first occurence has a 1 in the bottom right cell, and the second occurence has a 2 in the bottom right cell.

The sed command works, but php, or the exec command is interpreting it incorrectly I believe. Any ideas?

  • 写回答

1条回答 默认 最新

  • duanli0687 2012-02-16 16:00
    关注

    The solution for using exec with this particular regular expression was to use the php function, preg_replace()

    preg_replace("/\/s/./", "/", $filename);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog