SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Adding |target="_blank"| to each link in a variable?!

    Hello,

    Let's say I have the fallowing variable:

    PHP Code:
    $toto 'Example...
    <a href="http://www.example.com">example.com</a>
    Some HTML <b>Like bold</b> etc...
    <a href="http://www.example.net">example.net</a>
    <a href="http://www.example.org">example.org</a>
    etc...'

    I want to add to each link to fallowing code:

    Code:
    target="_blank"
    So each link be as fallow:

    Code:
    <a href="http://example.org"  target="_blank">example.org</a>
    How can I do that?

    Many thanks in advance...
    Why It Doesn't Work?!

  2. #2
    SitePoint Member Arvan's Avatar
    Join Date
    Aug 2003
    Location
    China
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <head>
    <base target="_blank">
    </head>

  3. #3
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I just want the link contained in the variable to open in a new window. NOT all the links in my site. that's illogical
    Why It Doesn't Work?!

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    define('REGEX', <<<EOS
    !        #start regex
    <        #find a tag start
    (        #start a regex match
    a        #find an anchor tag
    [^>]*    #followed by anything but a closing tag
    )        #end the match
    >        #followed by the close of the anchor tag
            #end regex, use extended whitespace modifier
    !x
    EOS
    );

    $new_toto preg_replace(REGEX'<\\1 target="_blank">'$toto); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks...
    Why It Doesn't Work?!


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
  •