Reputation: 839
I am trying to make a php script to output all values inside an hypehref=" ** " from a text file.
I'm having a hard time with the regular expression part.
This is what I have
$Vdata = file_get_contents('file.txt');
preg_match( '/hyperef="(.*?)"/', $Vdata, $match );
echo '<pre>'; print_r($match); echo '</pre>'
My result is this :
Array
(
[0] => hyperef="http://lookbook.nu/look/5709720-Choies-Silvery-Bag-Rosewholesale-Punk-Style/hype"
[1] => http://lookbook.nu/look/5709720-Choies-Silvery-Bag-Rosewholesale-Punk-Style/hype
)
The [0] is incorrect, it includes the part I am searching for... all I want is the result after the hypehref="
The second result [1] is correct
and my file should have given me about 10 results.. not just 2...
Any ideas why ? Thx
Upvotes: 0
Views: 3425
Reputation: 11194
$pattern = "/\bo'reilly\b/i"; // only O'Reilly
$ora_books = preg_grep($pattern, file('filed.txt'));
var_dump($ora_books);
example 2
$fh = fopen('/path/to/your/file.txt', 'r') or die($php_errormsg);
while (!feof($fh)) {
$line = fgets($fh);
if (preg_match($pattern, $line)) { $ora_books[ ] = $line; }
}
fclose($fh);
Upvotes: 0
Reputation: 10638
You can use preg_match_all
to find all matches. There you will also have the full part and only the value of the hyperef - but you can only use the former.
if (preg_match_all('/hyperef="(.*?)"/i', $Vdata, $result)) {
$matches = $result[1]; //only values inside quotation marks
print_r($matches);
} else
print "nothing found";
I added the if
for obvious reasons and the i
delimiter, so the pattern will ignore case sensitivity.
Upvotes: 1