hello help with string find & replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kobie
Forum Newbie
Posts: 1
Joined: Sun Oct 23, 2011 2:18 am

hello help with string find & replace

Post by kobie »

i want to search for a string that looks like:

Zpx Zpx;

while Z is N=(0,.......,100)

and replace the string to:

Zpx 0px 0px Zpx;
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: hello help with string find & replace

Post by genix2011 »

Hello,

you can use preg_replace to achieve this.

Code: Select all

<?php
$string = "2px 3px;";
$pattern = "/(\d)px (\d)px\;/i";
$replace = "$1px 0px 0px $2px;";

$result = preg_replace($pattern, $replace, $string);

echo $result;
Greets.
Post Reply