SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    May 2003
    Location
    Kiel, Germany
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [pregMatch / Replace] Link replacement with certain url

    Hello, I have a problem getting a preg-match/replace to work.

    I have a browserbased wysiwyg page-editor, and unfortunaly it does not have a target option for links, but it exactly fits my needs, thats why i want to stick with it.

    Only the links that are http://www.myserver.com/script.php?klicked=1234 should open in the same window, the other one should have a target="_blank" ...

    The search term for links that should not be replaced would be
    .php?klicked=
    Everything else can be targeted to open as "_blank" / new window.

    I tried a few things, but I just suck at preg matches/replaces ;-)

    Since the links allways have the whole URL even if localy,
    simply replacing
    a href="http://
    with
    a target="_blank" href="http://
    will not work, UBB style code is also no option.


    I am lost :-)
    Can anyone help?
    thanks!

    Marcus

    www.shawcartoon.com

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    If your links look like this: <a href="www.url.com/...">LinkText</a> then this should work:

    PHP Code:
    <?php

    // decide wether to add the target or not
    function addTarget($link$text) {
        if (
    strpos($link'.php?klicked=') === false)
            return 
    "<a href=\"$link\" target=\"_blank\">$text</a>";
        else
            return 
    "<a href=\"$link\">$text</a>";
    }

    $urls '<a href="http://www.myserver.com/script.php?klicked=1234">link</a> asdsadasd  
    <a href="http://www.myserver.com/script.php?klicked=1234">link2</a>

    <a href="http://www.anotherserver.com/script.php">link3</a>'
    ;

    echo 
    preg_replace('#<a href="(.+?)">(.+?)</a>#eis'"addTarget('\\1', '\\2')"$urls);  
      
    ?>
    Christian

  3. #3
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sitename 'mysite.com';
    $string '<a href="http://2mysite.com/testing...">Test</a>
    <a href="http://mysite.com/testing...">Test</a>'
    ;
    $string preg_replace('/<a(.*?)href=""(\'|" )\s*?http:\/\/(?!(www\.)?' preg_quote($sitename) . ')(.*?)\s*?\2([^>]*?)>/si''<a target="_blank"\1href=""\2http://\3\4\2\5>'$string);
    echo 
    $string
    Argh! 4th edit of this post, I hate vb

    EDIT: 5th edit!

    EDIT AGAIN: 6th edit!

    anyways that outputs:
    Code:
    <a target="_blank" href="http://2mysite.com/testing...">Test</a>
    <a href="http://mysite.com/testing...">Test</a>
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not going to edit that post again for fear of messing it up AGAIN, but every where there is an href="" should just be href= (no quotes)
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  5. #5
    SitePoint Member
    Join Date
    May 2003
    Location
    Kiel, Germany
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    thank you both for the fast reply cyborg from dh and Chris82!

    Opening only the local links in the same window is not enough, also the non-index.php files should open in a external window :-)

    The script from Chris82 hits the spot, thanks!
    Thank you both for putting so much time into this, I really apreacheate it!

    I'll mention you both on my site with link if you want ;-)

    Marcus, germany

    www.shawcartoon.com

  6. #6
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No need to mention me, lol, I didn't do anything for you, lol.

    (Thanks for the offer though, awful nice of you )
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  7. #7
    SitePoint Member
    Join Date
    May 2003
    Location
    Kiel, Germany
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you tried to help, and I might use your script for a other project ;-)
    Cya!

    Quote Originally Posted by cyborg from dh
    No need to mention me, lol, I didn't do anything for you, lol.

    (Thanks for the offer though, awful nice of you )

  8. #8
    SitePoint Member
    Join Date
    May 2003
    Location
    Kiel, Germany
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HELP Again!

    When i use the code to replace my links (see below or in the first reply above), it works fine... untill a Link wraps a image...
    Then the image will have slashes like using adslashes!
    width=\"50\" for example. This is strange...
    Whats going on? :-(

    Please help

    Code:
    <?php 
    function addTarget($link, $text) { 
        if (strpos($link, '.php?klicked=') === false) 
            return "<a href=\"$link\" target=\"_blank\">$text</a>"; 
        else 
            return "<a href=\"$link\">$text</a>"; 
    } 
    echo preg_replace('#<a href="(.+?)">(.+?)</a>#eis', "addTarget('\\1', '\\2')", $urls);   
       
    ?>

  9. #9
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Hm, I don't know why this happened, but here is a fix for the problem:

    PHP Code:
    <?php

    function addTarget($link$text) {
        
            
    // replace unwanted characters 
        
    $text str_replace("\\\""'"'$text);
        
    $link str_replace("\\\""'"'$link);

            
    // this replaces newlines in the link and text
        
    $link preg_replace("#(\r?\n)*#"''$link);
        
    $text preg_replace("#(\r?\n)*#"''$text);
        
        if (
    strpos($link'.php?klicked=') === false)
            return 
    "<a href=\"$link\" target=\"_blank\">$text</a>";
        else
            return 
    "<a href=\"$link\">$text</a>";
    }

    $urls '<a href="http://www.myserver.com/script.php?klicked=1234">link</a> asdsadasd  
    <a href="http://www.myserver.com/script.php?klicked=1234"><img src="http://www.hallo.de/test.jpg" 
    width="23" height="323"></a>

    <a href="http://www.myserver.com/script.php?klicked=1234"><img src="http://www.hallo.de/test.jpg" width="23" height="323"></a>

    <a href="http://www.anotherserver.com/script.php">link3</a>'
    ;

    echo 
    preg_replace('#<a href="(.+?)">(.+?)</a>#eis'"addTarget('\\1', '\\2')"$urls);

     
    ?>

  10. #10
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    when you use the /e modifier in REGEX, it auto addslashes() everything so that it can be passed correctly to functions.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  11. #11
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Thanks for the explanation. I would give you a "+" but vb doesn't let me
    Must "+" someone else before.

  12. #12
    SitePoint Member
    Join Date
    May 2003
    Location
    Kiel, Germany
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh ok i just used an strip slashes now, thanks for 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
  •