[英]Php regular expression echo characters after a string
我有一個很大的 .txt 文件,在 .txt 文件中,它包含數字 712 和其他字符。 例如 (712iu3 89234) 或 (712jnksuiosd)。 712之后的字符會發生變化,可能會有空格。 我有一個 php 腳本,可以逐行讀取文件。 我正在嘗試回顯 712 之后的所有字符 如果有空格我想刪除空格。 我只需要不包括空格的前 20 個字符。 到目前為止我已經嘗試過
$file = new SplFileObject("1.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
// Echo one line from the file.
echo $file->fgets();
}
// Unset the file to call __destruct(), closing the file handle.
$file = null;
嘗試使用下面的代碼
<?php
$file = new SplFileObject("test.txt");
// Loop until we reach the end of the file.
while (!$file->eof()) {
$newString = str_replace(' ', '', $file->fgets());
if (strpos($newString, '712') !== false) {
$strWith712 = substr($newString, 0, 20);
$post = strripos($strWith712, '712');
$str = substr ($strWith712, $post );
echo $str;
}
}
$file = null;
?>
這將替換空格,然后搜索帶有數字“712”的字符串,如果找到字符串,則打印數字后面的字母
strripos
函數用於檢查字符串在最后一個句子中的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.