Jump to content

[SOLVED] preg_replace() problems


Kasuke_Akira

Recommended Posts

Here's my code:

 

function cmc_convert ($str) {

    $simple_search = array(
 	'16',
	'12',
	'11',
	'10',
	'9',
	'8',
	'7',
	'6',
	'5',
	'4',
	'3',
	'2',
	'1',
	'W',
	'U',
	'B',
	'R',
	'G'
);

    $simple_replace = array(
 	'<img src=images/cmc_16.png>',
        '<img src=images/cmc_12.png>',
	'<img src=images/cmc_11.png>',
	'<img src=images/cmc_10.png>',
	'<img src=images/cmc_9.png>',
	'<img src=images/cmc_8.png>',
	'<img src=images/cmc_7.png>',
	'<img src=images/cmc_6.png>',
	'<img src=images/cmc_5.png>',
	'<img src=images/cmc_4.png>',
	'<img src=images/cmc_3.png>',
	'<img src=images/cmc_2.png>',
	'<img src=images/cmc_1.png>',
	'<img src=images/cmc_white.png>',
	'<img src=images/cmc_blue.png>',
	'<img src=images/cmc_black.png>',
	'<img src=images/cmc_red.png>',
	'<img src=images/cmc_green.png>'
);

    $str = preg_replace($simple_search, $simple_replace, $str);

    return $str;
} 

 

Basically....I don't know why it isn't working.  I realize it's probably in the $simple_search array, but I am not too good at string manipulation and was curious what I'm doing wrong.

 

If you know what I'm doing wrong and how to fix it, if you do fix it, can you explain to me why?  So I understand what's going on.  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/43327-solved-preg_replace-problems/
Share on other sites

preg patterns need a starting and ending delimiter.  Look at the following example from the manual:

 

$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);

 

In this case, the delimiter is "/", and appears at the start and end of every search pattern.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.