Jump to content

Quick Question


N-Bomb(Nerd)

Recommended Posts

I'm using preg_match to return a urls values, but some of the urls have the attribute title="text here" and some do not.

 

Is there a way to make my regex say that the whole title="text here" part is optional?

 

I'm currently using

'/<a href="forum\.php\?f=(.*?)" title=".*?">(.*?)<\/a>/is'

 

But that only returns the urls that have the title attribute..  :'(

Link to comment
Share on other sites

Try this:

/<a href="forum\.php\?f=(.*?)"[^>]*>(.*?)<\/a>/is

 

I just tried that and it's pulling items such as:

<a href="forum.php?f=106" title="">Games</a>

 

and

 

<a href="forum.php?f=107" title="Forums for discussion of all games.">All Games</a>

 

but it doesn't pull anything that looks like this:

 

<a href="forum.php?f=108">Other Games</a>

 

I'm not sure if it matters or not, but there's other markup right up against these anchor tags.

Link to comment
Share on other sites

Are you sure? It works for me:

 

$text =<<<TEXT
<a href="forum.php?f=108">Other Games</a>
TEXT;
preg_match_all('/<a href="forum\.php\?f=(.*?)"[^>]*>(.*?)<\/a>/is', $text, $matches);
print_r($matches);

 

Output:

 

Array
(
    [0] => Array
        (
            [0] => <a href="forum.php?f=108">Other Games</a> 
        )

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

    [2] => Array
        (
            [0] => Other Games
        )

)

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.