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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.