PHP Preg_replace with a * in the pattern

Hello experts.

I have a small bit of code...

and i am trying to do a simple preg_replace so that it becomes

Basically whatever the first item is i want it to replace the star.
I think the star is causing me issues as i cant get anything to work.

Any help would be gratefully accepted ...


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
Escape the * with a \ and always do your patterns in single quotes, not double quotes otherwise you wind up escaping the escape characters as well and regexes look bad enough to start with.

$pattern = '3,\*';

Beverley PortlockCommented:
Here you go, a better example

$pattern = '#(3),(\*)#';

$data = "Here is a test 3,* string";

echo  preg_replace( $pattern, '$1,$1', $data );

Open in new window


Here is a test 3,3 string
handypamAuthor Commented:
Thank you for your help so far.
The code works in isolation but you look at this test
It really does strange things to the string....

Any more help would get a huge :D from me... thanks in advance

	$test = 'test test ix="2,*" test test ix="3,*" test test ix="4,*" test test';
	echo "<br>Before:".$test;
	$t = preg_replace($find, $replace, $test);
	echo "<br>After:".$t;	


Open in new window

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Beverley PortlockCommented:
The pattern is too general - try this


"Any more help would get a huge :D from me"

I like hugs, but I guess I'll just have to settle for any points that come my way......

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Beverley PortlockCommented:
Oh - it was a HUGE :D not a HUG..... I misread it.

My apologies.....


handypamAuthor Commented:
Hurray it works

Faith in mankind = RESTORED!

Hugz + points
Beverley PortlockCommented:
Glad you're sorted. Thanks for the hugz as well as the points


One additional note - regex is greedy and will match as much as possible to a pattern like .* but you can tell it not to be greedy by using .*? so in your pattern I could have used


but if that test is always a number then it is safer to be specific which is why I replaced .* with [0-9]*
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.