www.webdeveloper.com
Results 1 to 6 of 6

Thread: preg_replace question

  1. #1
    Join Date
    Jun 2004
    Posts
    220

    preg_replace question

    Hi

    I'm trying to work out how to use the preg_replace (I think) function, but fail to understand this (I guess) simple stuff.

    What I want to do is to replace certain unknown strings inside a string with a third string (!). For example:

    Kill process apache on server mainsrv01

    to

    Kill process censored on server censored

    I cannot simply use str_replace since I do not know what will be in the text, but I know it will be like this with "wildcards":

    Kill process * on server *

    I have several such strings, so how should I do this?

    Thanks for your help and understanding
    Lubox

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by Lubox
    Kill process * on server *
    Does anything come after that or is it the end of the string.

  3. #3
    Join Date
    Jun 2004
    Posts
    220
    Hi

    Well, the string can be actually anything but there are one or two "places" in need of replacement.

    The string can be as in the original example (without anything behind) or it can be:

    Restart mainsrv01 at midnight

    where I want to replace it with:

    Restart censored at midnight

    Or:

    mainsrv01 encountered error in apache at 21:30

    with

    censored encountered error in censored at 21:30

    So, I'm trying to find a way to feed a function with for example three parameters, like:

    $result = ("Kill process * on server *","Kill process apache on server mainsrv01","censored");

    and $result would be "Kill process censored on server censored".

    Possible, you think?

    Thanks
    Lubox

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by Lubox
    Restart mainsrv01 at midnight
    mainsrv01 encountered error in apache at 21:30
    To use regular expressions you need regularity. Can you point out what is regular in the two strings you have posted? I know they both include mainsrv01 but you have already pointed out that will not be regular.

  5. #5
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    I may not be reading enough into this, but every example you've given seems simple enough. Why don't you just str_replace() the asterisks with "censored".

    In fact, don't even put the asterisks in there in the first place.

  6. #6
    Join Date
    Jun 2004
    Posts
    220
    Hi

    I think I'm failing to describe this properly or even see it clearly myself. I will get variables from an external function, which I need to edit before presenting. I know all of the texts, but I do not know some "parts" of it.

    For example: Kill process * on server *

    With *, I mean the original text can contain anything there, things I have no control over or knowledge about. But I do know that the other words will be there.

    This is what I have created now:

    PHP Code:
    //From external function:
    //Kill process apache on server mainsrv01
    $result preg_replace("/^Kill process (.*?) on server (.*?)$/","Kill process $removed on server $removed",$original_text);

    //and
    //From external function:
    //mainsrv01 encountered error in apache at 21:30

    $result preg_replace("/^(.*?) encountered error in (.*?) at (.*?)$/","$removed encountered error in $2 at $3",$original_text); 
    This seems to be doing the trick so far...but may be really stupid to do this

    Lubox

Thread Information

Users Browsing this Thread

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

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