Jump to content

Preg_replace


naescent

Recommended Posts

Hi,

 

I'm kind of new to PHP and very new to Regular Expressions and I was wondering if someone could help run through this code snippet from a callback function in a Joomla plugin called Affliates Feed.

 

So far I have this

is passed a pointer to variable item (I understand this is effectively the same as passing the variable but a lot less CPU intensive.

Creates an array with keys and values

 

Now the for loop I am stuck with, I understand the interation through the array. But what is preg_replace returning and why pass it back that way hasn't it already done its job?

 

Can any one help? And please feel free to be as basic as possible :help:

 

Thanks,

Adam

 

 

function gardening(&$item) {
   #renaming categories, not the most efficient use for example strtolower first, and use only lowercase in the rename array
   $rename=array(
      ' Gardening?' => '',
      'Climber Plants' => 'Climbing Plants',
      'Seeds.*Bulbs' => 'Seeds Bulbs',
      'Plant protection' => 'Horticultural goods',
      'Garden structures' => 'Garden buildings',);
   foreach ( $rename as $k => $v ) {
      list($item['menu_1'],$item['menu_2'])=preg_replace("#$k#",$v,array($item['menu_1'],$item['menu_2']));
   }
   $item['menu_1']=ucfirst(strtolower($item['menu_1']));
   $item['menu_2']=ucfirst(strtolower($item['menu_2']));
   $item['menu_3']=ucfirst(strtolower($item['menu_3']));
   $item['menu_4']=ucfirst(strtolower($item['menu_4']));
}

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

preg_replace returns an array if it is passed an array as the subject. What it's doing here is replacing $k with $v in $item['menu_1'] and in $item['menu_2'], then returning an array with the newly modified strings. Then the list() in front of it is putting those two strings in the returned array into their old variables. Preg_match doesn't directly modify the subject you pass it, it just returns the modified string.

Link to comment
https://forums.phpfreaks.com/topic/233155-preg_replace/#findComment-1199068
Share on other sites

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.