www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] regexp preg_replace, need help.

Hybrid View

  1. #1
    Join Date
    Sep 2008
    Posts
    61

    resolved [RESOLVED] regexp preg_replace, need help.

    Hey!

    I need help making a preg_replace() command to replace some stuff on my webpage.

    I've got a text like this:

    "click !:here:! to get some info about some stuff";

    and another variable, with the URL for a link, that will change often. So I want to use preg_replace to replace the !: and the :! with <a> and </a> tags.

    So I'm thinking something like this

    Code:
    preg_replace("/!:(.*):!/", "<a href='{$URL}'>$text</a>", $infotext);
    so the regexp, I don't really know if that's any good at all. It's just something I think might be correct. The thing is that the link could be anywhere in the string $infotext, and with any chars between the !: and the :!.. possible that I might want to change the symbols !: and :! too. I'm not really good at REGEXP.

    the $text variable shall be taken from the regexp search-pattern. Because the text between !: and :! will change quite often too.

    Hope this is possible.

    Cheers,
    Artheus
    Last edited by artheus; 02-08-2010 at 03:18 AM. Reason: some mistakes

  2. #2
    Join Date
    Sep 2008
    Posts
    61
    Well.. made it myself...

    Code:
    $infotext = "click !:here:!!";
    $url = "http://www.google.com";
    
    echo preg_replace("/!:(.*):!/", "<a href='" . $url . "'>$1</a>", $infotext);
    Works..

  3. #3
    Join Date
    Nov 2008
    Posts
    2,477
    If you have more than one placeholder your regex is going to match from the start of the first to end of the last. If this is an issue you will want to make the expression non-greedy - and maybe you would want to make it case insensitive too?

    Code:
    /!:(.*?):!/i

  4. #4
    Join Date
    Sep 2008
    Posts
    61
    Great! Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles