how to replace urls in a document (with regular expression)

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

    how to replace urls in a document (with regular expression)

    Hi-

    I'm completely stumped. I'm trying to write some code that will parse
    a file and rewrite it with all URLs replaced by something else.

    For example: if the file looks like this:

    <b>click here</b><a href="http://www.cnn.com">cl ick me</a>

    ... then the output should be this:

    <b>click here</b><a href="http://mysite?myurl=ww w.cnn.com">clic k
    me</a>

    Anyhelp on how to accomplish such thing would be really appreciated -
    I spent hours on this and I'm completely stuck.

    TIA,

    JJ
  • Andreas Paasch

    #2
    Re: how to replace urls in a document (with regular expression)

    Curious Expatriate wrote:
    [color=blue]
    > Hi-
    >
    > I'm completely stumped. I'm trying to write some code that will parse
    > a file and rewrite it with all URLs replaced by something else.
    >
    > For example: if the file looks like this:
    >
    > <b>click here</b><a href="http://www.cnn.com">cl ick me</a>
    >
    > .. then the output should be this:
    >
    > <b>click here</b><a href="http://mysite?myurl=ww w.cnn.com">clic k
    > me</a>
    >[/color]

    If I'm not mistaken, it's a string added to a string.

    //define the website url
    $string="http://www.cnn.com"
    // strip the text http:// (7 characters)
    $urlvariable=su bstr($string,7)
    //put it correctly together
    "http://mysite?myurl=". $urlvariable

    Should do what you want, right?

    /Andreas

    --
    Registeret Linux user #292411

    Comment

    • Andreas Paasch

      #3
      Re: how to replace urls in a document (with regular expression)

      Andreas Paasch wrote:
      [color=blue]
      > Curious Expatriate wrote:
      >[color=green]
      >> Hi-
      >>
      >> I'm completely stumped. I'm trying to write some code that will parse
      >> a file and rewrite it with all URLs replaced by something else.
      >>
      >> For example: if the file looks like this:
      >>
      >> <b>click here</b><a href="http://www.cnn.com">cl ick me</a>
      >>
      >> .. then the output should be this:
      >>
      >> <b>click here</b><a href="http://mysite?myurl=ww w.cnn.com">clic k
      >> me</a>
      >>[/color]
      >
      > If I'm not mistaken, it's a string added to a string.
      >
      > //define the website url
      > $string="http://www.cnn.com"
      > // strip the text http:// (7 characters)
      > $urlvariable=su bstr($string,7)
      > //put it correctly together
      > "http://mysite?myurl=". $urlvariable
      >
      > Should do what you want, right?
      >
      > /Andreas
      >[/color]

      Oops, doing this with regular expressions is not in my knowledge base,
      sorry.

      --
      Registeret Linux user #292411

      Comment

      • Justin Koivisto

        #4
        Re: how to replace urls in a document (with regular expression)

        Curious Expatriate wrote:
        [color=blue]
        > Hi-
        >
        > I'm completely stumped. I'm trying to write some code that will parse
        > a file and rewrite it with all URLs replaced by something else.
        >
        > For example: if the file looks like this:
        >
        > <b>click here</b><a href="http://www.cnn.com">cl ick me</a>
        >
        > .. then the output should be this:
        >
        > <b>click here</b><a href="http://mysite?myurl=ww w.cnn.com">clic k
        > me</a>
        >
        > Anyhelp on how to accomplish such thing would be really appreciated -
        > I spent hours on this and I'm completely stuck.
        >
        > TIA,
        >
        > JJ[/color]

        Try something like:
        $x=preg_replace ('/(http\:\/\/\S+)/i',"http://mysite?myurl=$1 "",$x);

        --
        Justin Koivisto - spam@koivi.com
        PHP POSTERS: Please use comp.lang.php for PHP related questions,
        alt.php* groups are not recommended.

        Comment

        Working...