Results 1 to 8 of 8

Thread: Get URL and replace

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Get URL and replace

    In this code:

    $String="Hi how are you http://mypage.com fine thanks http://youpage.com";

    How can I replace all sites in this var to any string, for example (STAR)

    I mean I want to let:
    $String="Hi how are you STAR fine thanks STAR";


    ---
    Like:
    replace
    (URL)http://site.com(/url)
    To
    <A href=http://site.com>http://site.com</A>

    in this forum

  2. #2
    ricmitch_uk
    Guest
    If you want something like the BB code used here then you can use:
    PHP Code:
    $string eregi_replace("\\[url=([^\\[]*)\\]([^\\[]*)\\[/url\\]","<a href=\"\\1\">\\2</a>",$string); 
    This will turn
    [url=http://www.mysite.com]Visit my site![/url]
    into
    <a href="http://www.mysite.com/">Visit my site!</a>


    If you just mean changing http://www.mysite.com/ to <a href="http://www.mysite.com/">http://www.mysite.com/</a>. Then you can use something like this:

    PHP Code:
    $string eregi_replace("\\[url]http://[/url]([^\\[]*)\\","<a href=\"\\1\">\\1</a>",$string); 
    HTH

  3. #3
    ricmitch_uk
    Guest
    If you want a BB code interpretting function, then I made this one for use with my PHP Portal for clans:
    PHP Code:
    function interpret($article) {
    #remove HTML code
    $article str_replace("?apostrophe¿""'"$article);
    $article  str_replace("<","&lt",$article);
    $article  str_replace(">","&gt",$article);
    #do UBB code
    $article str_replace("[b[b][/b]]","<b>",$article);
    $article str_replace("[/[b][/b]b]","</b>",$article);
    $article str_replace("[i[b][/b]]","<i>",$article);
    $article str_replace("[/i[b][/b]]","</i>",$article);
    $article str_replace("[[b][/b]u]","<u>",$article);
    $article str_replace("[/[b][/b]u]","</u>",$article);
    $article eregi_replace("\\[url=([^\\[]*)\\]([^\\[]*)\\[/url\\]","<a href=\"\\1\">\\2</a>",$article);
    $article eregi_replace("\\[icq=([^\\[]*)\\]([^\\[]*)\\[/icq\\]","<a href=\"http://wwp.icq.com/scripts/Search.dll?to=\\1\">\\2</a>",$article);
    $article eregi_replace("\\[colour=([^\\[]*)\\]([^\\[]*)\\[/colour\\]","<font color=\\1>\\2</font>",$article);
    $article eregi_replace("\\[email=([^\\[]*)\\]([^\\[]*)\\[/email\\]","<a href=\"mailto:\\1\">\\2</a>",$article);
    $article eregi_replace("\\[font=([^\\[]*)\\]""<font face=\"\\1\">"$article);
    $article eregi_replace("\\[colour=([^\\[]*)\\]""<font colour=\"\\1\">"$article);
    $article eregi_replace("\\[size=([^\\[]*)\\]""<font size=\"\\1\">"$article);
    $article eregi_replace("quote\\]","quote]",$article);  // make lower case
    $article str_replace("[qu[b][/b]ote]\r\n","<blockquote><smallfont>Quote:</smallfont><hr />",$article);
    $article str_replace("[quo[b][/b]te]","<blockquote><smallfont>Quote:</smallfont><hr />",$article);
    $article str_replace("[/qu[b][/b]ote]\r\n","<hr /></blockquote>",$article);
    $article str_replace("[/qu[b][/b]ote]","<hr /></blockquote>",$article);
    $article str_replace("[c[b][/b]ode]\r\n","<blockquote><smallfont>Code:</smallfont><hr /><font face=\"Courier\"><pre>",$article);
    $article str_replace("[cod[b][/b]e]","<blockquote><smallfont>Co[b][/b]de:</smallfont><hr /><font face=\"Courier\"><pre>",$article);
    $article str_replace("[/c[b][/b]ode]\r\n","</pre></font><hr /></blockquote>",$article);
    $article str_replace("[/code]","</pre></font><hr /></blockquote>",$article);
    #do smilies
    $article str_replace(":[b][/b])","<img src=\"../images/smile.gif\" border=\"0\" alt=\"Smile\" />",$article);
    $article str_replace(":[b][/b]-)","<img src=\"../images/smile.gif\" border=\"0\" alt=\"Smile\" />",$article);
    $article str_replace(":[b][/b]p","<img src=\"../images/tongue.gif\" border=\"0\" alt=\"Cheeky\" />",$article);
    $article str_replace(":[b][/b](","<img src=\"../images/sad.gif\" border=\"0\" alt=\"Sad\" />",$article);
    $article str_replace(":[b][/b]-(","<img src=\"../images/sad.gif\" border=\"0\" alt=\"Sad\" />",$article);
    $article str_replace(":[b][/b]o","<img src=\"../images/bored.gif\" border=\"0\" alt=\"Boring\" />",$article);
    $article str_replace(";[b][/b])","<img src=\"../images/wink.gif\" border=\"0\" alt=\"Wink\" />",$article);
    $article str_replace(";-[b][/b])","<img src=\"../images/wink.gif\" border=\"0\" alt=\"Wink\" />",$article);
    $article str_replace(":[b][/b]D","<img src=\"../images/grin.gif\" border=\"0\" alt=\"Big Grin\" />",$article);
    $article str_replace(":e[b][/b]ek:","<img src=\"../images/eek.gif\" border=\"0\" alt=\"Eek!\" />",$article);
    $article str_replace(":roll[b][/b]eyes:","<img src=\"../images/rolleyes.gif\" border=\"0\" alt=\"Rolls Eyes\" />",$article);
    $article str_replace(":c[b][/b]ool:","<img src=\"../images/cool.gif\" border=\"0\" alt=\"Cool\" />",$article);
    $article str_replace(":m[b][/b]ad:","<img src=\"../images/mad.gif\" border=\"0\" alt=\"Mad\" />",$article);
    $article str_replace(":confu[b][/b]sed:","<img src=\"../images/confused.gif\" border=\"0\" alt=\"Confused\" />",$article);
    #all done, return the stuff
    return $article;

    You can see it in action here.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    ricmitch_uk

    Thank you too much ricmitch_uk

  5. #5
    ricmitch_uk
    Guest
    That's OK. Glad to help

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    Error

    Please, ricmitch_uk
    I try code of (URL)http://site.com(/URL)
    and
    (url=http://www.mysite.com]Visit my site!(url)

    but it give me this error:
    Parse error: parse error in C:\apache\htdocs\add-subject.php on line --Line No-

    please help me

  7. #7
    ricmitch_uk
    Guest
    If you post the line in question, I might be able ot help you. Without it, I won't know where it's going wrong. All I know is that my code works. It's currently being used, on a website I admin.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259
    I think that there is an error in this code:

    Code:
    $string = eregi_replace("\<a href="http://([^\[" target="_blank">*)\]([^\[]*)\[/url\]","<a href=\"\1\">\2</a>",$string);

    but any way I got another code and it's work without error:

    Code:
    			$value = eregi_replace("[^src=\"](http://(([A-Za-z0-9~&=;\?%_#./\-])*)([a-zA-Z0-9/]))", " \\1",$value);
    Code:
    			$value = eregi_replace("[^http://](www\.(([A-Za-z0-9~&=;\?%_#./\-])*)([a-zA-Z0-9/]))", " \\1",$value);
    Code:
    			$value = eregi_replace("^(http://(([A-Za-z0-9~&=;\?%_.#/\-])*)([a-zA-Z0-9/]))", " \\1",$value);
    Code:
    			$value = eregi_replace("(\[url=)([A-Za-z0-9_~&=;\?:%@#./\-]+[A-Za-z0-9/])(\])", "<a href=\"\\2\" target=_blank>",$value);
    Code:
    			$value = eregi_replace("(\[/url\])", "</a>",$value);


    thank you too much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width