holyredbeard
holyredbeard

Reputation: 21248

If match on line 1 grab content on line 2

The code below picks every line that contains of the word "Homepage:" and extract what's after the semicolon. However what I want instead is that if match on the word "Homepage:" what's on the next line should be grabbed and put in a variable.

How could that be done?

if (preg_match( '/Homepage:/', $text, $match )) {
    $pieces = explode(':', $text);
    $urls[] = trim($pieces[1]);
}

Example:

Homepage:    <--- if match
http://www.fooland.com    <--- grab this and put in variable

Upvotes: 0

Views: 63

Answers (3)

Vinoth Babu
Vinoth Babu

Reputation: 6852

You can try with explode() in php

$urlResult = explode(':', $url)
echo $urlResult[0];

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336408

if (preg_match('/Homepage:.*\n(.*)/', $subject, $regs)) {
    $result = $regs[1];
}

Explanation:

Homepage: # Match Homepage:
.*        # Match any characters except linebreaks
\n        # Match a linebreak
(.*)      # Match and capture the contents of the entire line

Upvotes: 2

ravi
ravi

Reputation: 3424

you can try with regular expression '/Homepage:\n/'

Upvotes: 0

Related Questions