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
https://forums.phpfreaks.com/topic/205325-quick-question/
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
https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074640
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
https://forums.phpfreaks.com/topic/205325-quick-question/#findComment-1074650
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.