SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace with links help

    i've searched all over for this and can't seem to find it
    not very new with php but new with regexp...
    if someone can just help me haha

    i got a friendslist where class="link" on all links (could be up to 100 )

    the code:

    Code:
    <tr><td><a hrf='' " target='_blank'>phr0ss</a>  <a hrf='deletefriend?removed=phr0ss' ID='link' class="link" style='display:none;'>x</a></td></tr>
    
    <tr><td><a hrf='' " target='_blank'>proxikal</a>  <a hrf='deletefriend?removed=proxikal' ID='link' class="link" style='display:none;'>x</a></td></tr>
    the reason for hrf is it was saying it won't let me post links here...
    so bare with me its a bigger link

    now what i'm trying to do is getting regexp to remove for example: i remove proxikal...i need it to ONLY for proxikal link change the class name..
    or even remove it...either or..

    i've tried a few things and i got it to rename class="link" to what i wanted
    only problem is it changed every class="link" when i only wanted it for the one
    i removed: proxikal.
    if anyone could help me out, thanks so much

    this i what i tried lol

    Code:
    $removeit="deletefriend.php?removed=$removed' ID='link' class='link'>x</a>";
    
    $str = preg_replace('"/(href=(\"$removeit))|(http:\/\/)([\*\.]+) /class=\"([^\"]*)\"/i', 'class=\'myclass\'', $compile);
    but that was a defenate no go, as i assumed i was just doing anything
    at this point lol...but appreciate it anyone could help

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sorry if I've misunderstood, but if you're only trying to remove the class, can you not just use str_replace()?

    PHP Code:
    <?php
    $sString 
    '<a class="link" href="something.php?member=silverbulletuk">SilverBulletUK</a>';
    echo 
    str_replace('class="link"'''$sString); #<a  href="something.php?member=silverbulletuk">SilverBulletUK</a>
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Member
    Join Date
    May 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya, you just mis-understood me a little bit....

    ok example: there are 10 links on this page ALL links look like:
    Code:
    <a href=blah.php?remove="proxikal" class="link" id="link">proxikal</a>
    <a href=blah.php?remove="phr0ss" class="link" id="link">phr0ss</a>
    <a href=blah.php?remove="proxy" class="link" id="link">proxy</a>
    See how all those links have the same class name & id, if i were to use
    the above code you've given me, it would replace every class="link" wich is absolutely not what i'm looking for.

    and str_replace, won't remove html....
    to help you guys see what i mean, say i clicked Remove on my friends list
    to remove "phr0ss" right? ok...it simply would do this to "phr0ss" But not the other

    Code:
    <a href=blah.php?remove="proxikal" class="link" id="link">proxikal</a>
    <a href=blah.php?remove="phr0ss" class="changed" id="changed">phr0ss</a>
    <a href=blah.php?remove="proxy" class="link" id="link">proxy</a>
    understand a little easier?

    i'v been all over str_replace, ereg_replace, and none of these will remove html code, the code i am looking for would be more like

    Code:
    $removeit="'phr0ss' class='link' id='link'>phr0ss</a>";
    $done="'phr0ss' class='changed' id='changed'></a>";
    preg_replace('"/(href=(\"))|($removeit)([\*\.]+) /class=\"([^\"]*)\"/i', ' ', $compile);
    now i know thats incorrect, but it gives everyone a good idea of what i'm trying to do, this is far beyond str_replace & ereg_replace, more into Regular Expressions if i can only get preg_replace to replace $removeit
    with $done i'd be set...thanks

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo $pattern sprintf('#(<a[^>]*?)class="link"([^>]*?>%s</a>)#'preg_quote($needle'#'));
    echo 
    preg_replace($pattern'$1class="foo"$2'$haystack); 
    This will fail if your html has an attribute which contains the greater than character >
    for example
    title="i thnk apples > oranges"

    Take a look at preg_replace_callback() too. You might find it a bit easier to use for stuff like this because it gives you much more control, and you can perform tests and replacements in easier to manage steps.

  5. #5
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would something like this be along the lines of what you are looking for?

    Example:
    PHP Code:
    $str = <<<HTM
    <a href=blah.php?remove="proxikal" class="link" id="link">proxikal</a>
    <a href=blah.php?remove="phr0ss" class="link" id="link">phr0ss</a>
    <a href=blah.php?remove="proxy" class="link" id="link">proxy</a>
    HTM;

    function 
    changeClassIdStatus($a){
        return 
    preg_replace('#class="link" id="link">([^>]+)</a>#''class="changed" id="changed">$1</a>'$a[0]);
    }

    function 
    pattern($name){
        return 
    '#<a [^>]+remove="' $name '" class="link" id="link">'$name .'</a>#';
    }

    $targetName 'phr0ss';
    $str preg_replace_callback(pattern($targetName), 'changeClassIdStatus'$str);
    echo 
    $str
    Obviously, you would remove the $str heredoc syntax and change the $str variable listed elsewhere within the code to whatever string you are examining.

    Just a note: ids should be unique (in that, if there is the declaration say id="link", there should not be any other declaration of this id anywhere else in the that page's markup.. classes on the other are ok to repeat.. so there are definitely issues with the markup in question).

  6. #6
    SitePoint Member
    Join Date
    May 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, thanks guys got it - appreciate the help


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
  •