...

View Full Version : using preg_replace to change a string



LJackson
11-19-2010, 06:43 PM
Hi All

I am wondering if it is possible to change a string e.g
scrubs series 1 => scrubs series one
scrubs series one => scrubs series 1

basically if the string contains either a number below 10 or its equivelant word i want it changed to the other

e.g
1 => one
one => 1
2 => two
two => 2
etc...

is this possible?

also can i use preg replace to check for a couple of words e.g season,series and replace it with the other, so if the title was scrubs season 1 it would change it to scrubs series 1 and if it found series it would replace it with season?

many thanks

Luke

poyzn
11-19-2010, 06:57 PM
I don't know what do you have in the beginning, so i can suggest to use str_replace


$search = array(1,2,3,4,5);
$replace = array('one', 'two', 'three', 'four', 'five');
echo str_replace($search, $replace, 'scrubs series 1');


or



$search = array('1' => 'one',
'one' => '1');
echo strtr('scrubs series one', $search);
echo strtr('scrubs series 1', $search);

Keleth
11-19-2010, 07:17 PM
Yah, you could use preg_replace, but its inefficient for this I'd say. Go for str_replace like poyzn suggested.

LJackson
11-19-2010, 07:32 PM
ok thanks! :)

one little problem im having is is that if the string contains another number other in the array 32 for example it will change it to threetwo which i'd expect it to but as 32 is not in the array could this be prevented?

my code is like so

$find = array(1,2,3,4,5,6,7,8,9,10);
$replace = array('one', 'two', 'tree', 'four', 'five','six','seven','eight','nine','ten');
$search[3] = str_replace($find, $replace, $keywords);
$search[4] = str_replace($replace, $find, $keywords);


so technically 32 is not in there to be replaced by anything?
any ideas please

cheers

MattF
11-19-2010, 07:51 PM
You'd need to either space your numbers:



$find = array(' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 10 ');


or else use preg_replace for that level of control.

Rowsdower!
11-19-2010, 08:08 PM
Or do the str_replace on your $search array in a "for" loop and check the strlen() for each entry and only make the replacement for a particular entry when that length is less than 2 (in other words, when the number is greater than 9)...

It's not terribly fancy, but there is usually more than one way to skin a cat. If you aren't familiar with regex stuff then this would make for a much easier fix if you need to get going with this right away while you learn regex syntax.

kbluhm
11-19-2010, 09:07 PM
Word boundaries would be an effective solution.

A word boundary is marked as /b


$string = 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

$replacements = array(
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
);

foreach ( $replacements as $int => $word )
{
$string = preg_replace( '/\b' . $int . '\b/', $word, $string );
}

echo $string;


...or run preg_replace() once by preparing the expressions ahead of time:


$string = 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

$replacements = array(
'/\b1\b/' => 'one',
'/\b2\b/' => 'two',
'/\b3\b/' => 'three',
'/\b4\b/' => 'four',
'/\b5\b/' => 'five',
'/\b6\b/' => 'six',
'/\b7\b/' => 'seven',
'/\b8\b/' => 'eight',
'/\b9\b/' => 'nine',
'/\b10\b/' => 'ten',
);

$string = preg_replace( array_keys( $replacements ), $replacements, $string );

echo $string;

Output:


This is one, not 31. This is two or three, but not 32 or 33, got it?

LJackson
11-19-2010, 10:02 PM
wow didnt relise there were so many ways of acheiving it :D

thanks very much all!!!!!!!
Luke

LJackson
11-19-2010, 11:19 PM
Word boundaries would be an effective solution.

A word boundary is marked as /b


$string = 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

$replacements = array(
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
);

foreach ( $replacements as $int => $word )
{
$string = preg_replace( '/\b' . $int . '\b/', $word, $string );
}

echo $string;


...or run preg_replace() once by preparing the expressions ahead of time:


$string = 'This is 1, not 31. This is 2 or 3, but not 32 or 33... got it?';

$replacements = array(
'/\b1\b/' => 'one',
'/\b2\b/' => 'two',
'/\b3\b/' => 'three',
'/\b4\b/' => 'four',
'/\b5\b/' => 'five',
'/\b6\b/' => 'six',
'/\b7\b/' => 'seven',
'/\b8\b/' => 'eight',
'/\b9\b/' => 'nine',
'/\b10\b/' => 'ten',
);

$string = preg_replace( array_keys( $replacements ), $replacements, $string );

echo $string;

Output:


This is one, not 31. This is two or three, but not 32 or 33, got it?


Hi Kbluhm

the above works great :)
however i've tried changing it so that if it finds the word eg one then it will replace it with the key like so

$string = preg_replace($replacements, array_keys( $replacements ), $keywords );


but it returns a blank match? any ideas where im going wrong
thanks mate
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum