如何在文档上找到一个字符串,然后回显该字符串?
例如
该文件:
blah blah result.php?whatiwanttoecho blah
或:
blahhh result.php?whatiwanttoecho blah blah blah
在我想回应的内容之前,总会有"结果.php?
我正在寻找的最终结果:
$doc = "./doc.txt";
$doccontents = file_get_contents($file);
然后我需要帮助的代码最终结果为:
$result = 'whatiwanttoecho';
echo $result;
希望这是有道理的。谢谢 (:
GHi,
尝试以下代码中的示例:
$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
<?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>';
}
首先使用 $array =explode("?",$fullstring,2)
将整个字符串分成两部分,然后用 $urstring = explode(" ",$array[1],2)
分解字符串的第二部分,最后回显您想要echo $urstring[0];
的字符串怎么样?希望这能解决您的问题