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

Thread: PHP Highlight

  1. #1
    Regular Coder UrbanTwitch's Avatar
    Join Date
    Apr 2006
    Posts
    485
    Thanks
    44
    Thanked 1 Time in 1 Post

    PHP Highlight

    OK so I got this code and it only works when I put ONE word with NO space in it:

    PHP Code:
    $text "CodingForums are a great place to learn how to code. Also a good place to give help. CodingForums are blank!";
    $text preg_replace("/great/i""<span style=\"background:yellow\">\\0</span>"$text);
    echo 
    $text
    OK, if I put great learn instead of great... then it won't highlight. However, if I just put great. It will highlight great. Is there a way to make it highlight words spaced apart?
    Formerly MysticScript

    6+ years club

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can use different words if you use a | to separate them.
    Also, do you mean to highlight starting at great and ending with learn inclusive, or just the two words?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder UrbanTwitch's Avatar
    Join Date
    Apr 2006
    Posts
    485
    Thanks
    44
    Thanked 1 Time in 1 Post
    just two words.
    Formerly MysticScript

    6+ years club

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    /(great|learn)/
    Try that. Also, you can use an array and implode them on | to get a similar result.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder UrbanTwitch's Avatar
    Join Date
    Apr 2006
    Posts
    485
    Thanks
    44
    Thanked 1 Time in 1 Post
    But the user would have to type in the search box "great|learn" to search for multiple words... unless I use explode for the space and |.
    But after that... I'm lost. :\ I forgot how to count how many words were exploded. o_O
    Formerly MysticScript

    6+ years club

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can just replace whatever you want.
    If the user is expected to enter a space separated list, replace each space with a |, trimming any whitespace as necessary.
    Use count() or sizeof() to get the number of items from an explode (since its just an array).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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