Professional Web Applications Themes

how to replace urls in a doent (with regular expression) - PHP Development

Hi- I'm completely stumped. I'm trying to write some code that will p 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">click me</a> ... then the output should be this: <b>click here</b><a href="http://mysite?myurl=www.cnn.com">click 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...

  1. #1

    Default how to replace urls in a doent (with regular expression)

    Hi-

    I'm completely stumped. I'm trying to write some code that will p
    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">click me</a>

    ... then the output should be this:

    <b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
    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
    Curious Expatriate Guest

  2. #2

    Default Re: how to replace urls in a doent (with regular expression)

    Curious Expatriate wrote:
    > Hi-
    >
    > I'm completely stumped. I'm trying to write some code that will p
    > 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">click me</a>
    >
    > .. then the output should be this:
    >
    > <b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
    > me</a>
    >
    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=substr($string,7)
    //put it correctly together
    "http://mysite?myurl=".$urlvariable

    Should do what you want, right?

    /Andreas

    --
    Registeret Linux user #292411
    Andreas Paasch Guest

  3. #3

    Default Re: how to replace urls in a doent (with regular expression)

    Andreas Paasch wrote:
    > Curious Expatriate wrote:
    >
    >> Hi-
    >>
    >> I'm completely stumped. I'm trying to write some code that will p
    >> 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">click me</a>
    >>
    >> .. then the output should be this:
    >>
    >> <b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
    >> me</a>
    >>
    >
    > 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=substr($string,7)
    > //put it correctly together
    > "http://mysite?myurl=".$urlvariable
    >
    > Should do what you want, right?
    >
    > /Andreas
    >
    Oops, doing this with regular expressions is not in my knowledge base,
    sorry.

    --
    Registeret Linux user #292411
    Andreas Paasch Guest

  4. #4

    Default Re: how to replace urls in a doent (with regular expression)

    Curious Expatriate wrote:
    > Hi-
    >
    > I'm completely stumped. I'm trying to write some code that will p
    > 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">click me</a>
    >
    > .. then the output should be this:
    >
    > <b>click here</b><a href="http://mysite?myurl=www.cnn.com">click
    > 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
    Try something like:
    $x=preg_replace('/(http\:\/\/\S+)/i',"http://mysite?myurl=$1"",$x);

    --
    Justin Koivisto - [email]spamkoivi.com[/email]
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Justin Koivisto Guest

Similar Threads

  1. RegEx? To replace URLs in a string
    By FionaB in forum Coldfusion - Getting Started
    Replies: 0
    Last Post: June 9th, 10:22 AM
  2. help on regular expression
    By Madhu Reddy in forum PERL Beginners
    Replies: 5
    Last Post: January 28th, 01:31 AM
  3. Regular expression - replace non printable chars with spaces
    By Francesco in forum PHP Development
    Replies: 3
    Last Post: December 1st, 05:48 PM
  4. Replace with regular expression
    By Wiggins D'Anconia in forum PERL Beginners
    Replies: 3
    Last Post: November 14th, 03:17 PM
  5. [PHP] Regular Expression
    By Ralph Guzman in forum PHP Development
    Replies: 1
    Last Post: July 8th, 06:39 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139