pryashrma
pryashrma

Reputation: 102

Search string in text file that begins with particular word and ends with another specific word

I have a text file that contains various file absolute path along with other data. Now I need to write a php script to extract only the absolute file path excluding all the other text.

My file looks like

File:

E:\htdocs\DataSet\f1.php

Some text

    some more text

  * even more text
*

  * text again

      o E:\xampp\htdocs\DataSet\f2.php

      o E:\xampp\htdocs\DataSet\f3.php

I want to extract
E:\htdocs\DataSet\f1.php
E:\xampp\htdocs\DataSet\f2.php
E:\xampp\htdocs\DataSet\f3.php

Basically string/sentence that begins win "E:" and ends with ".php"

Although, the problem is simple but after searching and trying I am not able to find the solution right now...please help!

Upvotes: 0

Views: 455

Answers (3)

user3815506
user3815506

Reputation: 91

This might be work.

$filename = "Your file name";
$string = file_get_contents($filename);
preg_match_all("/E\:.*(?\.php)/", $string, $data);

Upvotes: 1

Chris
Chris

Reputation: 111

You can use regex for this:

$data = file_get_contents("path_to_your_data");
$pattern = '/E:.+\.php/';
preg_match_all($pattern, $data, $matches);

print_r($matches);

Upvotes: 1

Dalibor Karlović
Dalibor Karlović

Reputation: 568

You need regex with look ahead, this seems to work:

<?php
$s = file_get_contents('input.txt');
if (preg_match_all('/E\:.*(?!\.php)/', $s, $matches)) {
    $paths = current($matches);
}
var_dump($paths);

Upvotes: 1

Related Questions