PHP中的模式匹配-仅复制扩展名为.inp的word
我想在.inp之前显示字符串,并编写了一个php脚本,用于检查字符串是否包含.inp扩展名。PHP中的模式匹配-仅复制扩展名为.inp的word,php,regex,Php,Regex,我想在.inp之前显示字符串,并编写了一个php脚本,用于检查字符串是否包含.inp扩展名。 它将打印整行。。。。。它包含.inp扩展名 但我需要打印一个包含.inp扩展名的单词 例如,我有一行代码 “在您的计算机上找到了新的文件名xrs.inp” 我只需要打印xrs.inp 我写的剧本如下 <?php if (isset($_POST['ip10'])) { $connection = ssh2_connect($ip10, 22); ssh2_auth_passwor
它将打印整行。。。。。它包含.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扩展名您需要这样的正则表达式:
$found = preg_match("#([a-z]+)\.inp#", $output, $fileName);
$found = preg_match("#([a-z]+)".$check."#", $output, $fileName);
如果$found
为true,则使用$fileName[1]
。(fileName[0]
将包含整个文件名,包括扩展名,而fileName[1]
仅包含文件名)
问题是$checks
数组必须包含转义的特殊字符,因此它应该是:
$checks = array('\'.'.inp');
所以你可以这样使用它:
$found = preg_match("#([a-z]+)\.inp#", $output, $fileName);
$found = preg_match("#([a-z]+)".$check."#", $output, $fileName);
注意,只有当文件名之间没有空格时,这才有效。我稍微修改了你的解决方案 试试这个
$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>";
}
$output=“在您的计算机上找到新文件名asdsd xrs.inp”;
$array=explode(“,$output”);
$flag=false;
$filename=“”;
foreach($数组作为$check){
$place=strpos($check,.inp”);
如果($place>0){
$flag=true;
$filename=$check;
打破
}否则{
$flag=false;
}
}
如果($flag){
echo.inp文件正在该服务器上运行。“
”;
echo“文件名:”.$filename;
}否则{
echo“不存在任何.inp文件”。
;
}
如果文件名有空格怎么办?这与之有什么关系?@MartinPrikryl他正在从ssh连接获取字符串,但与问题无关,indead…上面的脚本用于通过ssh获取连接,$stream在其上执行命令。,其结果值将作为$output变量的输入…..无论输入字符串可能是什么,如果文件名中没有空格,您将使用上述脚本获得文件名。我不知道$stram变量的结果。你可能得检查一下