Need help with preg_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
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Need help with preg_replace

Post by fastfingertips »

I have following code:

Code: Select all

$arrPatterns['element'] = '/<<:(.+?):>>/se';
$strContent = preg_replace($arrPatterns['element'], "process_element('$1','".$arrPageDetails."')", $strContent);
But instead to give an array to process_element, $arrPageDetails is coming as a string with value array. Removing slashes causes other problems.

What do i have to do to make $arrPageDetails to be viewed as array?
walkekev
Forum Newbie
Posts: 4
Joined: Wed Mar 22, 2006 7:38 pm

Post by walkekev »

I dont get what you want to do overall. But to make that an array do this

Code: Select all

<?php
$arrPatterns = array("/","<","<",":","(",".","+","?",")",":",">",">","/","s",",e");
?>
That will make each Charecter a diffrent element in the array. each element should be covered by "elemtnt" seperated by a comma
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

It looks like you're trying to replace <<:(anything here):>> .

str_replace might be a better option for you as it runs quicker - no need to run the regular expression engine.

If that won't work, try taking the call to process_element() out of quotes. It won't get run if it's in quotes. To be honest, I've never seen this done before, but I guess theoretically it should work.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

I solved, i added an /

Code: Select all

$strContent = preg_replace($arrPatterns['element'], "process_element('$1',\$arrPageDetails)", $strContent);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, kind of makes my advice seem pointless eh? :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply