我想在.inp之前显示字符串,我写了一个php脚本来检查字符串是否包含.inp扩展名。
它将打印整行.....包含.inp扩展名
但是我需要打印一个包含.inp扩展名的单词
举个例子,我有一条线
"新文件名为xrs.inp,可在您的机器上找到"
我只需要打印xrs.inp
我写的脚本如下:
<?php
if (isset($_POST['ip10'])) {
$connection = ssh2_connect($ip10, 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'ps -efa | grep g09 ');
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
$array = explode(" ", $output);
$checks = array('.inp');
foreach ($checks as $check) {
$place = strpos($output, $check);
if (!empty($place)) {
echo '.inp file is running on this server' . "<br>";
exit;
} else {
echo "not present any .inp file" . "<br>";
}
}
fclose($errorStream);
fclose($stream);
}
?>
请帮我打印名称后跟.inp扩展名
答案 0 :(得分:0)
你需要这样的正则表达式:
$found = preg_match("#([a-z]+)\.inp#", $output, $fileName);
如果$fileName[1]
为真,请使用$found
。 (fileName[0]
将包含整个文件名,包括扩展名,而fileName[1]
仅包含文件名)
问题是你的$checks
数组必须包含转义的特殊字符,所以它应该是:
$checks = array('\'.'.inp');
所以你可以像这样使用它:
$found = preg_match("#([a-z]+)".$check."#", $output, $fileName);
答案 1 :(得分:0)
请注意,只有当您的文件名之间没有空格时,这才有效。我稍微修改了你的解决方案。
试试这个
$output="new file name as asdsd xrs.inp is found on your machine";
$array =explode(" ",$output);
$flag=false;
$filename="";
foreach($array as $check) {
$place = strpos($check, ".inp");
if ($place>0) {
$flag=true;
$filename=$check;
break;
} else {
$flag=false;
}
}
if($flag){
echo '.inp file is running on this server'."<br>";
echo "File Name:".$filename;
}else{
echo "not present any .inp file"."<br>";
}