string http:// conwert to <a href=''>host</a>

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

    string http:// conwert to <a href=''>host</a>

    hi

    in form TEXTAREA i have text like:

    bla bla http://www.google.com/bla/bla/bla text text text text


    now how to make from this text like:

    bla bla ['http://www.google.com'] text text text text


    ['']-> url to http://www.google.com/bla/bla/bla


    I wrote this script:
    Were error is?


    ps. sorry , my english is not good.:)
    <?


    function string_url($tex t) {

    $arr=explode("\ n", $text);
    $j=0;


    foreach( $arr as $val)
    {

    if( strlen($val) > 1 )
    {


    $arr2=explode(" ", $val);
    $i=0;
    foreach( $arr2 as $val2)
    {

    $findme = 'http';
    $pos = strpos($val2, $findme);
    if ($pos === false)
    {

    $arr2[$i] = $val2;
    }

    else {

    $val2=parse_url ($val2);
    $host=$val2['host'];
    $path=$val2['path'];
    $scheme=$val2['scheme'];
    $tekst="<a HREF=\"$scheme://";
    $tekst.=$host;
    $tekst.=$path;
    $tekst.=">$host </a>";
    $arr2[$i]=$tekst;
    }

    $i++;
    }//koniec 2 forech (slowa)
    $slowa=implode( " ", $arr2);
    $arr[$j] = $slowa;

    }


    else
    {
    $arr[$j] = $val;
    }

    $j++;
    }//koniec 1 forech
    $text_nowy= implode("\n", $arr);
    return $text_nowy;
    }




    if ( $tresc) {

    echo string_url($tre sc);


    } else {


    print "<FORM METHOD=POST > </B><B>title:</B><BR>";
    print "<TEXTAREA NAME=tresc ROWS=30 COLS=60[color=blue]
    >$tresc</TEXTAREA><BR>";[/color]
    print "<br><INPUT TYPE=\"submit\" VALUE=\"add!\"> <br><br>";
    print "</FORM>";
    }


    ?>

  • Markus L.

    #2
    Re: string http:// conwert to &lt;a href=''&gt;host &lt;/a&gt;

    Am Fri, 14 Oct 2005 12:23:24 -0700 schrieb specjal:
    [color=blue]
    > hi
    >
    > in form TEXTAREA i have text like:
    >
    > bla bla http://www.google.com/bla/bla/bla text text text text
    >
    >
    > now how to make from this text like:
    >
    > bla bla ['http://www.google.com'] text text text text
    >
    >
    > ['']-> url to http://www.google.com/bla/bla/bla
    >
    >
    > I wrote this script:
    > Were error is?
    >
    >
    > ps. sorry , my english is not good.:)
    > <?
    >
    >
    > function string_url($tex t) {
    >
    > $arr=explode("\ n", $text);
    > $j=0;
    >
    >
    > foreach( $arr as $val)
    > {
    >
    > if( strlen($val) > 1 )
    > {
    >
    >
    > $arr2=explode(" ", $val);
    > $i=0;
    > foreach( $arr2 as $val2)
    > {
    >
    > $findme = 'http';
    > $pos = strpos($val2, $findme);
    > if ($pos === false)
    > {
    >
    > $arr2[$i] = $val2;
    > }
    >
    > else {
    >
    > $val2=parse_url ($val2);
    > $host=$val2['host'];
    > $path=$val2['path'];
    > $scheme=$val2['scheme'];
    > $tekst="<a HREF=\"$scheme://";
    > $tekst.=$host;
    > $tekst.=$path;
    > $tekst.=">$host </a>";
    > $arr2[$i]=$tekst;
    > }
    >
    > $i++;
    > }//koniec 2 forech (slowa)
    > $slowa=implode( " ", $arr2);
    > $arr[$j] = $slowa;
    >
    > }
    >
    >
    > else
    > {
    > $arr[$j] = $val;
    > }
    >
    > $j++;
    > }//koniec 1 forech
    > $text_nowy= implode("\n", $arr);
    > return $text_nowy;
    > }
    >
    >
    >
    >
    > if ( $tresc) {
    >
    > echo string_url($tre sc);
    >
    >
    > } else {
    >
    >
    > print "<FORM METHOD=POST > </B><B>title:</B><BR>";
    > print "<TEXTAREA NAME=tresc ROWS=30 COLS=60[color=green]
    >>$tresc</TEXTAREA><BR>";[/color]
    > print "<br><INPUT TYPE=\"submit\" VALUE=\"add!\"> <br><br>";
    > print "</FORM>";
    > }
    >
    >
    > ?>[/color]

    $tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
    href="$1$2$3$4" >$1$2$3$4</a>', $tresc);

    --
    -------------------------------------------------------
    Try this: SCA the Smart Class Archive for PHP

    -------------------------------------------------------

    Comment

    • specjal

      #3
      Re: string http:// conwert to &lt;a href=''&gt;host &lt;/a&gt;

      Warning: Unknown modifier '/'
      what is wrong?

      $tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
      href="$1$2$3$4" >$1$2$3$4</a>', tresc);

      Comment

      • Janwillem Borleffs

        #4
        Re: string http:// conwert to &lt;a href=''&gt;host &lt;/a&gt;

        specjal wrote:[color=blue]
        > Warning: Unknown modifier '/'
        > what is wrong?
        >
        > $tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
        > href="$1$2$3$4" >$1$2$3$4</a>', tresc);
        >[/color]

        The slashes are used as modifiers to mark the beginning and the end of the
        pattern, but they are also used in the pattern itself.

        You can fix this by escaping the slashes in the pattern:

        $tresc = preg_replace('/(http)(s?)(:\/\/)([^ ]+)/', '<a
        href="$1$2$3$4" >$1$2$3$4</a>', tresc);

        Or by using another modifier:

        $tresc = preg_replace('# (http)(s?)(://)([^ ]+)#', '<a
        href="$1$2$3$4" >$1$2$3$4</a>', tresc);


        JW



        Comment

        • Toby Inkster

          #5
          Re: string http:// conwert to &lt;a href=''&gt;host &lt;/a&gt;

          Janwillem Borleffs wrote:[color=blue]
          > specjal wrote:
          >[color=green]
          >> Warning: Unknown modifier '/'
          >>
          >> $tresc = preg_replace('/(http)(s?)(://)([^ ]+)/', '<a
          >> href="$1$2$3$4" >$1$2$3$4</a>', tresc);[/color]
          >
          > The slashes are used as modifiers to mark the beginning and the end of the
          > pattern, but they are also used in the pattern itself.[/color]

          No -- the slashes are used as *delimiters*. The *modifiers* are any extra
          characters that occur after the end delimiter. For example:

          /foo/i

          will match 'foo' case-insensitively. In this example, the 'i' is a
          modifier -- it modifies the behaviour of the regular expression to make it
          case-insensitive.

          Specjal's warning is caused by the third slash. PHP sees the second slash
          as the end delimiter, which makes any trailing characters modifiers.

          --
          Toby A Inkster BSc (Hons) ARCS
          Contact Me ~ http://tobyinkster.co.uk/contact

          Comment

          • Janwillem Borleffs

            #6
            Re: string http:// conwert to &lt;a href=''&gt;host &lt;/a&gt;

            Toby Inkster wrote:[color=blue]
            > No -- the slashes are used as *delimiters*. The *modifiers* are any
            > extra characters that occur after the end delimiter.
            >[/color]

            Yeah, you're right.


            JW



            Comment

            Working...