Advanced Search

Results 1 to 6 of 6

Thread: PHP Find Symbol and Replace It With The Word It Goes With

  1. #1
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile PHP Find Symbol and Replace It With The Word It Goes With

    I want to create a script that will read a string and whenever the hashtag symbol (#) appears, it replaces it with a link. How could I do this?

    I've only found scripts that just replace the # and not the word it's attached to.

    Thanks!

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by M2com View Post
    I've only found scripts that just replace the # and not the word it's attached to.
    well, that's exactly what you described:
    Quote Originally Posted by M2com View Post
    I want to create a script that will read a string and whenever the hashtag symbol (#) appears, it replaces it with a link
    So, can you explain a little further? What do you want to accomplish?

    Code:
    1) #word  =>  <a href="http://example.com">link</a>
    2) #word  =>  <a href="http://example.com">word</a>
    3) #word  =>  <a href="http://word">link</a>
    4) #word  =>  <a href="http://word">word</a>
    5) something else?
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. #3
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the reply, the second line in your code looks about what I want.

    #word -> <a href='website.com/page.php?h=$word'>#word</a>

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    read about preg_replace().

    PHP Code:
    <?php #example
    $text "I want a link to the #homepage here.";

    $newText preg_replace'/#([\w]+)/','<a href="http://website.com/page.php?h=$1">$1</a>',$text );

    print 
    $newText;
    /* outputs:
        I want a link to the <a href="http://website.com/page.php?h=homepage">homepage</a> here.
    */

    to explain the regex:
    Code:
    /#([\w]+)/
    
    /           begin pattern
      #         a literal "#" symbol
      (         begin subpattern 
                  (this allows you to backreference the match later with $1
        [       begin character class
                  (a type of character to match)
          \w    "word character"
                  (most characters, that don't separate words)
        ]       end character class
        +       one or more of the previous characters
      )         end subpattern
                  (taken together, this means the "#" only qualifies if there are one or more "word characters" after it)
    /           end pattern
    Last edited by traq; 04-12-2013 at 07:36 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  5. #5
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Worked like a charm! Thanks very much for the snippet and full explanation, very helpful!

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    no prob, glad I could help.

    If your question has been answered, please mark your thread "resolved":
    • On your original post (post #1), click [edit], then click [go advanced].
    • In the "thread prefix" box, select "Resolved".
    • Click [save changes].
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

Similar Threads

  1. multi-browser javascript word replace
    By nhoss2 in forum JavaScript
    Replies: 16
    Last Post: 11-29-2010, 03:40 PM
  2. DWMR:WHEN I press FIND&REPLACE eg xxxzzz.com with zzz.com and choose ALL SITE REPLACE
    By leonidassavvides in forum Computer hardware and software
    Replies: 0
    Last Post: 04-16-2009, 06:32 PM
  3. Resolved Cannot find symbol error
    By dan0 in forum Java
    Replies: 1
    Last Post: 02-28-2009, 08:14 PM
  4. Find and Replace
    By Sinopa in forum Looking for such a script or service
    Replies: 2
    Last Post: 12-26-2008, 01:37 PM
  5. Find-replace form validation
    By techno_race in forum The lounge
    Replies: 0
    Last Post: 03-06-2007, 12:09 AM

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
  •