【问题标题】:How to find a string on document and then echo the string?如何在文档上找到一个字符串然后回显该字符串?
【发布时间】:2012-07-01 14:54:57
【问题描述】:

如何在文档中找到一个字符串,然后回显该字符串?

例如,

文件:

blah blah result.php?whatiwanttoecho blah

或:

blahhh result.php?whatiwanttoecho blah blah blah

总会有'result.php?'在我想要回应之前。

我正在寻找的最终结果:

$doc = "./doc.txt";

$doccontents = file_get_contents($file);

然后是我需要帮助的代码,其最终结果为:

$result = 'whatiwanttoecho';
echo $result;

希望这是有道理的。谢谢(:

【问题讨论】:

    标签: php string document file-get-contents


    【解决方案1】:

    你好,

    试试这个代码中的例子:

    $doccontents = 'blahhh result.php?whatiwanttoecho blah blah blah';
    $rgxp = '/result.php\?([^ ]+)/i';
    if(preg_match($rgxp, $doccontents, $mc)) $result = $mc[1];
    else $result = 'No match';
    echo $result;      // whatiwanttoecho
    

    【讨论】:

      【解决方案2】:
      <?php
      error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
      $string= 'blah blah result.php?whatiwanttoecho blah';
      $string= 'blahhh result.php?whatiwanttoecho blah blah blah';
      preg_match_all('/result\.php\?([.\w\W]*?)[\s\n]/ui', $string, $matches);
      foreach(end($matches) as $key=> $value){
          print '<pre>'.print_r($value, 1).'</pre>';
      }
      

      【讨论】:

      • 如果字符串以result.php?whatiwanttoecho 结尾,则您的正则表达式不匹配(另请参阅this link)。
      • 抱歉,但与 CoursesWeb 解决方案相反,如果 result.php?whatiwanttoecho 位于字符串末尾,则您的正则表达式仍然不匹配(另请参阅 this link)。
      【解决方案3】:

      如何首先使用$array =explode("?",$fullstring,2) 将完整的字符串分解为两部分,然后使用$urstring = explode(" ",$array[1],2) 分解字符串的第二部分,最后回显您想要的字符串echo $urstring[0];? 希望这能解决你的问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多