SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get parameters from <a> tag

    How can i get all parameters from an <a> tag?

    need to have the following result
    $content = 'class="idz" href="http://www.domain1.com"';

    PHP Code:
    $content 'asdasd <a class="idz" href="http://www.domain1.com">View this</a> hhh ';

    $pattern '/#<a(.*)>View this</a>#ims/'
    $content preg_replace($pattern'$1'$content);

    var_dump(content); 

  2. #2
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did:
    PHP Code:
    $content '<a href="http://localhost" class="links" id="home">Home</a>'

    $pattern '/<[a]{1}(.*)?>(.*)<\/[a]{1}>/';   // sloppy regex
    $content trim(preg_replace($pattern'$1'$content)); 

    $attributes explode(" "$content);
    $attrArray = array();

    foreach (
    $attributes as $attribute)
    {
        list(
    $attr$value) = explode("="$attribute2);
        
    $attrArray[$attr] = str_replace("\""""$value);

    and the resulting array looked like:
    PHP Code:
    Array
    (
        [
    href] => http://localhost
        
    [class] => links
        
    [id] => home


  3. #3
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Zurev,

    Many thanks, seems it is almost working...

    How can only check for Home link
    $pattern = '/<[a]{1}(.*)?>(Home)<\/[a]{1}>/'; // sloppy regex
    seems an error in this regex

    PHP Code:
    $content '<a href="http://localhost" class="links" id="home">Home</a>... <a href="http://localhost/links" class="links" id="links">Links</a>'

    $pattern '/<[a]{1}(.*)?>(Home)<\/[a]{1}>/';   // sloppy regex
    $content trim(preg_replace($pattern'$1'$content)); 

    $attributes explode(" "$content);
    $attrArray = array();

    foreach (
    $attributes as $attribute)
    {
        list(
    $attr$value) = explode("="$attribute2);
        
    $attrArray[$attr] = str_replace("\""""$value);



    var_dump($attrArray); 

  4. #4
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DOMDocument is probably a safer bet:


    PHP Code:
    $html '<a href="http://localhost" class="links" id="home">Home</a>... <a href="http://localhost/links" class="links" id="links">Links</a>'
    $searchWord 'Home';
    $attributes = array();
    $parameterString '';

    $doc = new DOMDocument(); 
    $doc->loadHTML($html);

    foreach(
    $doc->getElementsByTagName('a') as $link) {
        if (
    $link->nodeValue == $searchWord) {
            foreach(
    $link->attributes as $attr) {
                
    $attributes[$attr->name] = $attr->value;
                
    $parameterString .= ' '.$attr->name.'="'.$attr->value.'"';
            }
        }
    }

    // show results
    echo '<pre>';
    print_r($attributes);
    print_r($parameterString);
    echo 
    '</pre>'
    aaron-fisher.com - PHP articles and more


Bookmarks

Posting Permissions

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