1

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

例如,

文件:

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;

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

4

3 回答 3

2

嗨,

尝试此代码中的示例:

$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
于 2012-07-01T15:17:01.683 回答
1
<?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>';
}
于 2012-07-01T15:10:51.953 回答
0

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

于 2012-07-01T15:13:44.707 回答