Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts

    str_replace help!

    Hello!!

    I am currently writing a news script that retrievs news from a db.

    Now I am making it so like if you do ; ) it makes a and so forth, its working great, but can anybody tell me how do make it so if i like do this:

    [img=http://url here]

    How can i make it so that the script then gets the image?

    Thanks!

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For this you will need regex. I am FAR to regex challenged to make anything, but I could try...

    PHP Code:

    $message 
    "Your message with [img=http://mysite.com/myimage.gif] image tags";

    $regex "\\[img=(.*)\\]";

    $img preg_replace($regex"<img src=\"$1\" border="0" />"$message); 
    It may or may not work , But I hope this helps.

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks i will give it a go!

    can anybody also give a little more description into this?

    thanks

    james

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can give you a bit of a description of what regex is:

    Regex, or Regular Expressions, are used in PHP (and many other languages) to match a string pattern...

    Take this example:

    [a-zA-Z0-9]{1}

    Will match 1 character that is: a-z, A-Z or 0-9.

    There are many other operators:


    ? Will match 1 or 0 instances of a pattern:

    [a-z]? Will match 1 or 0 characters that are a-z. If there is more than one instance, it will not match it.


    And +, {,x},{x,},{x},{x,x}

    I hope this gives you a brief I idea. I cant think of anywhere to look for info.

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I kinda cheated when I did this, I did it the very long way around, but I could understand it.

    PHP Code:
    $replace[0] = "/:p/";
        
    $replace[1] = "/:\)\)/";
        
    $replace[2] = "/:D/";
        
    $replace[3] = "/:\?/";
        
    $replace[4] = "/\;\)/";
        
    $replace[5] = "/:\)/";
        
    $replace[6] = "/:o/";
        
    $replace[7] = "/:-D/";
        
    $replace[8] = "/8\)/";
        
    $replace[9] = "/:\\\'\(/";
        
    $replace[10] = "/:eek:/";
        
    $replace[11] = "/:evil:/";
        
    $replace[12] = "/:frown:/";
        
    $replace[13] = "/:mad:/";
        
    $replace[14] = "/:\|/";
        
    $replace[15] = "/:\\$/";
        
    $replace[16] = "/:rolleyes:/";
        
    $replace[17] = "/:\(/";
        
    $replace[18] = "/:twist:/";
        
    $replace[19] = "/;\)/";

        
    $replaced[0] = "<img src='images/icon_razz.gif' title='Razz' alt='Razz'/>";
        
    $replaced[1] = "<img src='images/icon_lol.gif' title='LOL' alt='LOL'/>";
        
    $replaced[2] = "<img src='images/icon_biggrin.gif' title='Big grin' alt='Big grin'/>";
        
    $replaced[3] = "<img src='images/icon_confused.gif' title='Confused' alt='Confused'/>";
        
    $replaced[4] = "<img src='images/icon_wink.gif' title='Wink' alt='Wink'/>";
        
    $replaced[5] = "<img src='images/icon_smile.gif' title='Smile' alt='Smile' />";
        
    $replaced[6] = "<img src='images/icon_eek.gif' title='Eek' alt='Eek' />";
        
    $replaced[7] = "<img src='images/icon_cheesygrin.gif' title='Cheesy grin' alt='Cheesy grin' />";
        
    $replaced[8] = "<img src='images/icon_cool.gif' title='Cool' alt='Cool' />";
        
    $replaced[9] = "<img src='images/icon_cry.gif' title='Cry' alt='Cry' />";
        
    $replaced[10] = "<img src='images/icon_eek.gif' title='Eek' alt='Eek' />";
        
    $replaced[11] = "<img src='images/icon_evil.gif' title='Evil' alt='Evil' />";
        
    $replaced[12] = "<img src='images/icon_frown.gif' title='Frown' alt='Frown' />";
        
    $replaced[13] = "<img src='images/icon_mad.gif' title='Mad' alt='Mad' />";
        
    $replaced[14] = "<img src='images/icon_neutral.gif' title='Neutral' alt='Neutral' />";
        
    $replaced[15] = "<img src='images/icon_redface.gif' title='Red face' alt='Red face' />";
        
    $replaced[16] = "<img src='images/icon_rolleyes.gif' title='Roll eyes' alt='Roll eyes' />";
        
    $replaced[17] = "<img src='images/icon_sad.gif' title='Sad' alt='Sad' />";
        
    $replaced[18] = "<img src='images/icon_twisted.gif' title='Twisted' alt='Twisted' />";
        
    $replaced[19] = "<img src='images/icon_wink.gif' title='Wink' alt='Wink' />";

        
    $entry preg_replace($replace,$replaced,$entry); 
    <edit>forgot to disable smilies</edit>
    <edit part="2">Forum's stripped out slashes now, so you'll get errors :rolleyes: Add a backslash before the ),(,' and ? You might have to add more than one backslash before the '</edit>
    Last edited by Nightfire; 04-20-2003 at 01:33 PM.

  • #6
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    ok thanks!

    but how would i make this so it automatically finds the : ) and stuff in the database


  •  

    Posting Permissions

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