replace all _self with _blank

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bboldi

    replace all _self with _blank

    Hi!

    I have a string containing a HMTL page.I need to insert target="_blank"
    where targer attribute is not present within <a... TAG, and replace
    other target attributes - example target="_self" - to _blank. Can
    anyone help me? Thanx in advance, and sorry for my english.

  • Marcin Dobrucki

    #2
    Re: replace all _self with _blank

    bboldi wrote:[color=blue]
    > Hi!
    >
    > I have a string containing a HMTL page.I need to insert target="_blank"
    > where targer attribute is not present within <a... TAG, and replace
    > other target attributes - example target="_self" - to _blank. Can
    > anyone help me? Thanx in advance, and sorry for my english.[/color]

    how about:
    Replace all occurrences of the search string with the replacement string

    Perform a regular expression search and replace

    Comment

    • bboldi

      #3
      Re: replace all _self with _blank

      Ok. I know about these functions, the problem is: i need to determine
      if target attribute is alredy defined, if not, i need to insert one, if
      yes, then replace the variable of target attribute i have problen with
      "finding if target att. is defined and inserting one if not" part. any
      idea? thanx in advance.

      Comment

      • Marcin Dobrucki

        #4
        Re: replace all _self with _blank

        bboldi wrote:[color=blue]
        > Ok. I know about these functions, the problem is: i need to determine
        > if target attribute is alredy defined, if not, i need to insert one, if
        > yes, then replace the variable of target attribute i have problen with
        > "finding if target att. is defined and inserting one if not" part. any
        > idea? thanx in advance.[/color]

        I hope this is not some homework...

        if (preg_match("ta rget", $url)) {
        // search and replace
        }
        else {
        // crude but effective, url in form:
        // <a href="someplace .html" option="value"> link text</a>
        $url_array = explode('>', $url);
        $new_url = $url_array[0].' target="your_ta rget">'.$url_ar ray[1] . '>';
        }

        To do a bit more work, search for the position of the first '>', and
        then insert a string into the url (eg. with stripos() and
        substr_replace( )) just before that place.

        Of if you really want to put effort in, tokenize the whole url, and
        then parse the arguments as you see fit.

        /Marcin

        Comment

        Working...