Jump to content

[SOLVED] Preg_Match_All Problem


N-Bomb(Nerd)

Recommended Posts

What exactly would I be doing wrong with this expression?

 

preg_match_all('/<a href="lyrics\.php\?id=(.*?)">(.*?)<img src="(.*?)"><\/a>/is', $String, $Title);

 

What I'm mainly going after is the second search, however the other two are always random.

 

Example:

<a href="lyrics.php?id=5352">Limp Bizkit - Boiler<img src="hot.gif"></a>

Link to comment
Share on other sites

Try:

 

$s = "Limp Bizkit - Boiler";
$pattern = '~^(.*)$~i';
preg_match_all($pattern, $s, $matches);
print_r($matches);

?>

 

- You were pretty close (not that mine's perfect) but I made some changes.

- If 'id' is always going to be an integer you should use ([0-9]*).

- You should use \s for whitespaces.

 

In the $matches array you have to go through and figure out what each key goes to each match.  The array dump would look like this:

 

Array
(
    [0] => Array
        (
            [0] => Limp Bizkit - Boiler
        )

    [1] => Array
        (
            [0] => 5352
        )

    [2] => Array
        (
            [0] => Limp Bizkit - Boiler
        )

    [3] => Array
        (
            [0] => hot.gif
        )

)

 

Hope this helps, good luck.

Link to comment
Share on other sites

What exactly would I be doing wrong with this expression?

 

preg_match_all('/<a href="lyrics\.php\?id=(.*?)">(.*?)<img src="(.*?)"><\/a>/is', $String, $Title);

 

What I'm mainly going after is the second search, however the other two are always random.

 

Example:

<a href="lyrics.php?id=5352">Limp Bizkit - Boiler<img src="hot.gif"></a>

 

Your code works fine when I'm testing it. What went wrong for you? If you're only looking for one match, you should use preg_match() instead of preg_match_all() for better efficiency.

 

@Maq

- If you're sure that you're matching a space, it's perfectly fine to simply use a literal space in the pattern. But yeah, \s is more versatile (matching most/all kinds of whitespace) and I often prefer it over literal spaces in these type of cases.

- You added start and end of string (^ and $) to your pattern. That would keep the pattern from matching the data if it is part of a larger source code, which I would guess it is.

- You also removed the s modifier (or didn't add it). It's always a good idea to use the s modifier when using the dot, if a newline should appear within the desired match :)

 

@OP

You're a Bizkit fan? Watched the live stream of their concert @ Download Festival last weekend - freakin' awesome gig! 8)

Link to comment
Share on other sites

- If you're sure that you're matching a space, it's perfectly fine to simply use a literal space in the pattern. But yeah, \s is more versatile (matching most/all kinds of whitespace) and I often prefer it over literal spaces in these type of cases.

 

I agree.

 

- You added start and end of string (^ and $) to your pattern. That would keep the pattern from matching the data if it is part of a larger source code, which I would guess it is.

 

Yes I know.  Not sure why I did that.

 

- You also removed the s modifier (or didn't add it). It's always a good idea to use the s modifier when using the dot, if a newline should appear within the desired match :)

 

Yeah, I forgot to add it back in.  Thanks for the info.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.