1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem to match a regular expression

Discussion in 'PHP' started by Morphos, Sep 26, 2011.

  1. #1
    Hi guys,

    I'm already very desperate with a regular expression trying to match a piece of text, please help:

    This is the text:

    <OPTION value="25">Anglicky</OPTION> <OPTION value="19">Auto, moto</OPTION> <OPTION value="39">Stavebníctvo</OPTION> <OPTION value="39">staveb</OPTION> <OPTION value="26">Školstvo</OPTION>


    And I need to select only <OPTION value="39">Stavebníctvo</OPTION> however not literally - its parts change as well. So far I got this:

    (?<=\<OPTION).*?(?i)staveb.*?(?=\</OPTION\>)
    SEMrush
    The problem is that it matches also the text before my desired selection. Pleeease help me!!! thank you very much
     
    Morphos, Sep 26, 2011 IP
    SEMrush
  2. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #2
    try this
    $s = '<OPTION value="25">Anglicky</OPTION> <OPTION value="19">Auto, moto</OPTION> <OPTION value="39">Stavebníctvo</OPTION> <OPTION value="39">staveb</OPTION> <OPTION value="26">Školstvo</OPTION>';
    $pattern = '#(<option value="\d+">staveb[^<]+</option>)#si';
    if (preg_match($pattern, $s, $m))
    {
            print_r($m);
    }
    
    
    
    Code (markup):
     
    gvre, Sep 26, 2011 IP