/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@bokehFeb 05.2007 — Kill process * on server *[/QUOTE]Does anything come after that or is it the end of the string.
Copy linkTweet thisAlerts:
@LuboxauthorFeb 05.2007 — 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
Copy linkTweet thisAlerts:
@bokehFeb 05.2007 — Restart mainsrv01 at midnight

mainsrv01 encountered error in apache at 21:30[/QUOTE]
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.
Copy linkTweet thisAlerts:
@NightShift58Feb 06.2007 — 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.
Copy linkTweet thisAlerts:
@LuboxauthorFeb 06.2007 — 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:

[code=php]
//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);


[/code]


This seems to be doing the trick so far...but may be really stupid to do this

Lubox
×

Success!

Help @Lubox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...