regex help needed

Discussion in 'PHP' started by tamilsoft, Jun 16, 2008.

  1. #1
    Hi,

    I have an article stored in db with html tags. I want to apply target="_blank" for all the links that don't have target="_blank" in that article.

    Thanks in advance
     
    tamilsoft, Jun 16, 2008 IP
  2. sample4u

    sample4u Well-Known Member

    Messages:
    683
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    108
    #2
    for $15 i can do this work.
     
    sample4u, Jun 16, 2008 IP
  3. joffer

    joffer Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    15$ ... dude heres the free version :)
    If your html is in $text use this :
    
    $text = str_replace('<a href="','<a target="_blank" href="',$text);
    
    Code (markup):
     
    joffer, Jun 16, 2008 IP
  4. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, I already using this way.., but I want to apply only links which dont have target="_blank"
     
    tamilsoft, Jun 16, 2008 IP
  5. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    
    if(!stripos($text,"target=\"_blank\""))
    {
    $text = str_replace('<a href="','<a target="_blank" href="',$text);
    }
    
    PHP:
     
    rohan_shenoy, Jun 17, 2008 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    If your document is not frame based (which is a big no no in the first place), you should NEVER use target anyway. It is not in the strict HTML specification at all, and hacing it serves no discernable purpose.
     
    blueparukia, Jun 17, 2008 IP
  7. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    how do you open up things in a new window then?
    thats the only reason i use target:"_blank"
     
    X.Homer.X, Jun 17, 2008 IP
  8. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #8
    The other reason for him using target="_blank" could be that he does not want people to leave his site when they click on that follows another site.
     
    rohan_shenoy, Jun 17, 2008 IP
  9. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Okey
    I have asked using regex not using string functions, any way thanks for your suggestion, I have fixed the problem using regex. :)
     
    tamilsoft, Jun 17, 2008 IP
  10. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #10
    ^Couldn't you have told that in your previous reply itself?
     
    rohan_shenoy, Jun 17, 2008 IP
  11. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #11
    
    how do you open up things in a new window then?
    Code (markup):
    Accessible Javascript.

    It is not part of the strict HTML standard and is therefore invalid HTML.
     
    blueparukia, Jun 19, 2008 IP
  12. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #12
    Perspective.

    Even if it's 'bad' HTML - If it's supported by all the browsers (and it is.) then whats wrong with it.

    I'm expecting a reply to this that this is 'Bad coding' and 'Bad standards' and it is. But target is a much more reliable way than javascript (which won't work if it's disabled.)

    This is the theory Google and other large companies use when coding their sites, they work on all browsers, but they don't meet standards. But so what, as long as you have the correct output - do standards really matter?

    Let's have a friendly debate.

    Dan
     
    Danltn, Jun 19, 2008 IP