#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    How to make [^.....] for sting instead for chars...


    How to make my regexp to catch the following strings:
    <A class="classname" href="http://domain/subfolders">some text <BR /> more text</A>
    <A href="http://domain/subfolders" class=classname>some text <BR /> more text</A>
    <A href="http://domain/subfolders" class="classname">some text more text</A>
    but not to catch the following:
    <A class="classname" href="http://domain/subfolders">some text more text</A>more text</A>

    to catch the first 3 strings i tried this:
    '@<a[^>]*classname[^>]*>.+</a>@i'
    Unfortunately it catches the second one too. I cannot figure it how to replace .+ so <br /> (or any other tag) to pass except </a> tag.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    Aren't you really just checking that there aren't two </a>s in the string?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0
    Originally Posted by requinix
    Aren't you really just checking that there aren't two </a>s in the string?
    Here is an example what i'm trying to do:

    PHP Code:

    //I have a string i need to parse but for this example i'll create one from the 4 strings in my first post:

    $string  '<A class="classname" href="http://domain/subfolders">some text <BR /> more text</A>';
    $string .= '<A href="http://domain/subfolders" class=classname>some text <BR /> more text</A>';
    $string .= '<A href="http://domain/subfolders" class="classname">some text more text</A>';
    $string .= '<A class="classname" href="http://domain/subfolders">some text more text</A>more text</A>';



    $array_categories=preg_match_all('@<a[^>]*classname[^>]*>.+</a>@i'$string$matches);
    print_r($matches); 
    i need to get output like this:
    Array
    (
    [0] => Array
    (
    [0] => <A class="classname" href="http://domain/subfolders">some text <BR /> more text</A>
    [1] => <A href="http://domain/subfolders" class=classname>some text <BR /> more text</A>
    [2] => <A href="http://domain/subfolders" class="classname">some text more text</A>
    )
    )
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    Oh, that's all?
    PHP Code:
    preg_match_all('#<a[^>]*>.*?</a>#i'$string$matches
    If you want the class=classname,
    PHP Code:
    preg_match_all('#<a[^>]*?class="classname"[^>]*>.*?</a>#'$string$matches
    Much more and it'll be easier to do this with something like DOMDocument.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0
    wow it worked
    i cannot still get the logic but that ? fixed it exactly as i need it.
    Thank you
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    The first one looks for a <a, grabs everything up to the next >, then finds everything up to the first </a> it encounters.

    The second one looks for a <a, grabs some stuff, finds a class="classname", grabs everything else up to the next >, then finds everything up to the first </a> it encounters.

IMN logo majestic logo threadwatch logo seochat tools logo