Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    280
    Thanks
    25
    Thanked 9 Times in 9 Posts

    regular expression

    Hi all, sorry for so many posts.

    Any who,

    while(!feof($fpointer))
    {
    $line = fgets($fpointer);

    $attribute = array('class="', 'id="', 'name="', 'alt="', 'content="', 'width="', 'height="', 'href="', 'type="', 'src="', 'media="');
    foreach($attribute as $a)
    {
    $moo = preg_quote($a, "/");
    $tag = preg_match_all("/$a([^-]+)\"/", $line, $matches);
    if($tag)
    {
    foreach($matches[0] as $a)
    {
    echo "Line: ".$line_num." stuff: ".$a."<br />\n";
    }
    }
    }

    $line_num++;
    }
    fclose($fpointer);



    this is the code, it reads each line of a doc, and each line has a html tag on it. i want to gather the attributes.

    PHP Code:

    $tag 
    preg_match_all("/$a([^-]+)\"/"$line$matches); 
    gets the whole lot name="sdf" href="dfd" alt="sdfdf"

    how do i stop " from being alowd in ([^-]+) that bit?

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Code:
    [^-]+
    Means find anything except a hyphen that is one or more so to add " inside to make (you've got escape the " though with a backslash)
    Code:
    [^-\"]+
    and then put that into your new code.

    Although the code you're using looks very inefficient, could you tell me what it does maybe I can make a better one.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •