2012-07-01 42 views
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; 

希望這是有道理的。謝謝(:

回答

2

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 
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>'; 
} 
+0

如果STRI ng以'result.php?whatiwanttoecho'結尾,你的正則表達式不匹配(另見[this link](http://ideone.com/FC4p1))。 – scessor

+0

我已更新! – HanhNghien

+0

對不起,但與CoursesWeb解決方案相反,如果'result.php?whatiwanttoecho'位於字符串的末尾,您的regex仍然不匹配(另請參閱[此鏈接](http://ideone.com/jer3B))。 – scessor

0

如何首先使用$array =explode("?",$fullstring,2)爆炸兩個部分完整的字符串,然後用爆炸的$urstring = explode(" ",$array[1],2)字符串的第二部分,最後呼應字符串ü要echo $urstring[0]; ?? 希望這能解決問題烏爾

相關問題