0

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

2
  • Switch to preg_match_all and check the manual... Commented Jan 5, 2014 at 1:26
  • Also, you have a typo, you've used "hyperef" in the preg_match, but stated "hyperhref" in the description Commented Jan 5, 2014 at 1:32

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.