preg_replace (I Guess?)

Discussion in 'PHP' started by Shaokan, Jul 17, 2009.

  1. #1
    Hello there, I'm really in trouble with this preg_replace and any kind of help is greatly appreciated.

    Here is what I want: I have box where people may reply topics, and whenever they write (See: xxx yyy zzz) I want that xxx yyy zzz to be a link, but not the "see:" part. So basically, every word between See: and " ) " will become a link.

    I hope I was clear enough.
    Thank you.
     
    Shaokan, Jul 17, 2009 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Try this:

    
    $string = preg_replace('/See: (xxx yyy zzz)/iUsm','<a href=$1>$1<\/a>',$string);
    
    PHP:
    not tested
     
    ThePHPMaster, Jul 17, 2009 IP
  3. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sorry master, not working=) thanks tho for your concern
     
    Shaokan, Jul 17, 2009 IP
  4. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    here is my string;

    $string = '(see:aX maasdasd asdasd asdasaaa) see: xxx yyy zzz (SEE: HELLO WORD)over the lazy dog. see:asd.com';

    using your code the part starting from xxx becomes all a link, not only xxx yyy zzz. For example in this string, I want that "aX maasdasd asdasd asdasaaa" and "HELLO WORD" to become link as they are written within paranthesises.

    I know, that's not an easy one but I think it's not impossible. Javascript will do too, I don't really care about the language. I just need the script
     
    Shaokan, Jul 17, 2009 IP
  5. neegeris

    neegeris Banned

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ThePHPMaster your ThePHPnoob not master,your code is not working and your code is not safe at all, even 3 years old kids will know how to bypass your code, and do XSS, but I know you dont know what is XSS, first learn security of php ,and then go here and give your code..

    Shakon
    You need know what symbols urls can contain, then you write pattern what will accept only if url contains allowed symbols.. its easy..
     
    neegeris, Jul 17, 2009 IP
  6. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    well write me down a script if its easy.

    This is exactly what I want;

    (See: digitalpoint) ==becomes==> (See: digitalpoint)

    Please don't write me a script like preg_replace("/digitalpoint/", "< a href bla bla bla", $string). I don't want only digitalpoint to be link but whatever it comes there, if someone writes dictipedie I want (See: dictipedie)

    I hope I was clear enough.
     
    Shaokan, Jul 17, 2009 IP
  7. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I have no idea what the hell neegeris was saying...

    Anyway, here ya go:


    $string = '(see:aX maasdasd asdasd asdasaaa) see: xxx yyy zzz (SEE: HELLO WORD)over the lazy dog. see:asd.com';
    
    preg_match_all("/\(see:(.*?)\)/i", $string, $matches);
    
    foreach ($matches[1] as $match){
    echo "<a href=http://www.google.com>".$match."</a><br>";
    }
    PHP:
     
    Goramba, Jul 17, 2009 IP
  8. god404

    god404 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    how do i grab this with pregmatch
    
    s1.addVariable("file","http://aol.com/aoluser/aolvideos/4a614672/4a4dfa0a04ee6.flv");
    Code (markup):
    any help ? i did try file= doesnt work any other idea?
     
    god404, Jul 17, 2009 IP
  9. neegeris

    neegeris Banned

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Goramba your code is not secure too.. it will parse any js as link, if it would be wrote right
     
    neegeris, Jul 18, 2009 IP
  10. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    goramba thank you mate. However, even though that's the closest one to my goal, your code strips other texts away which is something that I don't want at all. Thank you again for your concern.
     
    Shaokan, Jul 18, 2009 IP
  11. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I'm starting to think you're a troll. First, I just worked to solve his problem, not write a whole security class for him. If we get a working piece of code we can talk security.

    Second, you keep chiming in but with nothing to contribute - just working on your arrogant post count?




    Hmm... With the string you posted and your request it seems to do what you asked. Maybe if you post a real example and the exact output you want I can modify it.
     
    Goramba, Jul 18, 2009 IP
  12. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I believe this is the clearest example I can make.

    (See: dictipedie) ==becomes==> (See: dictipedie)

    However, that's not only that. If someone would write (See: Dictipedie - Spread Your Knowledge!) the link will contain spaces which at the end will be encoded to %20, instead of this I want "_" in the < a > tag's href section instead of spaces.

    Here is what I exactly want.

    (See: Dictipedie - Spread Your Knowledge!) ==becomes==> [html version] See: <a href="topic.php?t=Dictipedie_-_Spread_Your_Knowledge!">Dictipedie - Spread Your Knowledge!</a> [output version] (See: Dictipedie - Spread Your Knowledge!)

    Well because I am not native and the problem is a bit complicated it's a bit hard to explain it properly, but I hope the example was clear enough.

    Thank you again for your concern.
     
    Shaokan, Jul 18, 2009 IP
  13. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Ok, simple change:

    $string = '(See: Dictipedie - Spread Your Knowledge!)';
    
    preg_match_all("/\(see: (.*?)\)/i", $string, $matches);
    foreach ($matches[1] as $match){
    $nospace=str_replace(" ", "_", $match);
    echo '(See: <a href="topic.php?t='.$nospace.'">'.$match.'</a>)';
    }
    
    PHP:

    RESULT:

    (See: <a href="topic.php?t=Dictipedie_-_Spread_Your_Knowledge!">Dictipedie - Spread Your Knowledge!</a>)
     
    Goramba, Jul 18, 2009 IP
  14. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #14
    Didn't have my server up at that time, now this is tested and working fine:

    
    <?php
    $string = 'sdfsdflk lkjsdf See: xxx yyy zzz ;kljs;dkf ;kjsdfoijoij234 See: xxx yyy zzz kjsdf;klj See: xxx yyy zzz';
    $string = preg_replace('/See: (xxx yyy zzz)/U','<a href="$1">$1</a>',$string);
    echo $string;
    ?>
    
    PHP:
     
    ThePHPMaster, Jul 18, 2009 IP
  15. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    yeah thank you bro=) I'm so close now, just a minor problem, what if the string was

    $content = 'word word _anotherword _anotherword asda (see: me) (see: asdasd hheheh) (see: hehehe hihiih ha) ( see:asdasd) see: hehehe ( see : asd1231 1231 <>)';

    I want the string to remain as it is and the (see: x) parts to change.

    lol I know I am bothering a lot but I really need this code =)
     
    Shaokan, Jul 18, 2009 IP
  16. Shaokan

    Shaokan Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    it's ok mate I finally made it=) thank you bro for your help!
     
    Shaokan, Jul 18, 2009 IP
  17. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #17
    My mistake for not reading the question correctly. Glad to hear you fixed the problem.
     
    ThePHPMaster, Jul 18, 2009 IP