Help with Perl Compatible Regular Expression

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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Help with Perl Compatible Regular Expression

Post by Takuma »

Hi, I have a file which contain these string:- {CONTENT} or {TITLE} etc. I created the regular expression to find these strings:-

Code: Select all

/\{{$template_variable}\}/
(I think this is right... but not sure) $template_vairable contains CONTENT etc. Now after finding out it has the string I need to replace it with an string like "Hello" etc. I used preg_replace() but can't work out how to use them (I've read the manual...). Please help.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you're looking for complete strings? no wildcards etc?
then you may consider using str_replace. The syntax is almost the same for preg_replace.

The first parameter (search) is what you're looking for (in your case "{CONTENT}"or "{TITLE}") and the second (replace) what you'd like it to be replaced by ("Hello" or "goodbye" or ...). The last parameter (subject) is the source for searching. The result is returned, subject-string not altered.

Any occurence of <search> in <subject> will be replaced. In case of preg_replace the whole matching substring is replaced.

Each parameter may be an array of strings.
Any occurence of one string in the <search>-array is replaced by the corresponding entry in the <replace>-array (in each()-order). If you have less entries in <replace> than in <search> '' is used (the substring is deleted).
This will be done for all strings in <subject>.

Code: Select all

<?php
$search = array('&#123;CONTENT&#125;', '&#123;TITLE&#125;');
$replace = array('Hello', 'goodbye');

$subject = array(
			'You say &#123;TITLE&#125; and I say &#123;CONTENT&#125;',
			'&#123;CONTENT&#125; &#123;CONTENT&#125;',
			'I don''t know why you say &#123;TITLE&#125;, I say &#123;CONTENT&#125;',
			'&#123;CONTENT&#125; &#123;CONTENT&#125;',
			'I don''t know why you say &#123;TITLE&#125;, I say &#123;CONTENT&#125;.'
		);

$subject = str_replace($search, $replace, $subject);
print('<pre>'); print_r($subject); print('</pre>');
?>
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Thanks again volka but what would be the most fastest way to replace these string?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I put the source of this page in a file eight times and used it as source for str_replace and preg_replace.
$search = array('!{CONTENT}!', '!{TITLE}!'); (without ! for str_replace)
$replace = array('Hello', 'goodbye');

average elapsed time:
0.0468119382858 str_replace
0.0571789741516 preg_replace
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

str_replace is alot faster, and easier to use.. it just doesnt have regular expressions, so it doesnt have to startup the regular expression engines... its for replacing more concrete things
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

thanks guys... (girls?)
Post Reply