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

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<html><body>
<?php
	$search = array('!foo!', '!allice!');
	$replace = array('bar', 'bob');
	$subject = array('foo allice foo', 'foo allice foo');
	
	print_r(preg_replace($search, $replace, $subject, 1));
?>
</body></html>
-->
Array ( [0] => bar bob foo [1] => bar bob foo )
you see: it limits the use of each search entry in each source line :(
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

yea but my subject is an array already. it was a file that was read into an array.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do you need that array or was it just the comfort of file() ? ;)
if there's no (other) need for that array try fopen() and fread()
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

i used file() because i couldnt get fread to print right and i used the array created by file() to print out only certain lines of the file using for and each.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hmm...no good advices left :(

maybe you should read the file with fread, do the replace and explode it to an array.

maybe some other hints:
- preg_replace_callback
- if you're replacing constants strings (using PCRE only because of the limit-option) you can do this by strpos, substr and .
- others know a good trick :D
Post Reply