str_replace not working in array

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
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

str_replace not working in array

Post by mesz »

Dear All,
When intoducing an array my str_replace function seems to have stopped working:

Code: Select all

<?PHP 
$data = file('news.dat'); 
{ 
$arrayFiles = implode($data, $fin); 
$string = "$data"; 
$pattern = '!/\\[img\\](.+?)\\[\\/img\\]/!i, !/\[URL=(.*?)\](.*)\[\/URL\]/i'; 
$replacement = '!<img src="\\1">!, !<a class="link3" //href="http://\\1">\\2</a>!'; 
$fin= str_replace($pattern, $replacement, $string); 
print $arrayFiles; 
} 
?>


all is printed to the screen,
except the [img] and [ url ] str_replacement has not worked.
Does anybody know why?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

str_replace() does not take a pattern - it is looking for the exact text:

Code: Select all

!/\\&#1111;img\\](.+?)\\&#1111;\\/img\\]/!i
which does not exist in your text file.

Do you mean to use preg_replace()?

Mac
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

I did what you suggested and changed the code:

Code: Select all

<?PHP 
$data = file('news.dat'); 
{ 
$string = "$data"; 
$pattern = !/\\[img\\](.+?)\\[\\/img\\]/!i, !/\[URL=(.*?)\](.*)\[\/URL\]/i'; 
$replacement = '!<img src="\\1">!, !<a class="link3" //href="http://\\1">\\2</a>!'; 
print preg_replace($pattern, $replacement, $data); 
} 
?>
however all the text is still printing to the screen with no image replacements, or links.
I have been doing this for 4 days and have read all about preg and str_replace, but I just cannot fathom this.
The features worked sepeartely until I put them in an array?
Cheers for any light anyone can shed,any...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Sorry, didn't notice this first time but you need to put the patterns and replacements into arrays. You also need to make sure your delimiters are consistant and you don't need delimiters on the replacement text, so instead of:

Code: Select all

$pattern = '!/[img](.+?)[/img]/!i, !/[url=(.*?)](.*)[/url]/i'; 
$replacement = '!<img src="\\1">!, !<a class="link3" //href="http://\\1">\\2</a>!';
you need something like:

Code: Select all

$pattern = array('!\[img\](.+?)\[/img\]!i', '!\[URL=(.*?)\](.*)\[/URL\]!i'); 
$replacement = array('<img src="\\1">', '<a class="link3" //href="http://\\1">\\2</a>');
otherwise you are looking to replace the following pattern:

Code: Select all

&#1111;img](.+?)&#1111;/img]/!i, !/&#1111;URL=(.*?)](.*)&#1111;/URL]
not

Code: Select all

&#1111;img](.+?)&#1111;/img]
// and
&#1111;URL=(.*?)](.*)&#1111;/URL]
Mac
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Sorry to be utilising your time like this, one day I'm sure I'll be able to help you with something...
but I have changed my script to

Code: Select all

<?PHP 
$data = file('news.dat'); 
{ 
$string = "$data"; 
$pattern = array('!\[img\](.+?)\[/img\]!i', '!\[URL=(.*?)\](.*)\[/URL\]!i'); 
$replacement = array('<img src="\\1">', '<a class="link3" //href="http://\\1">\\2</a>'); 
print preg_replace($pattern, $replacement, $data); 
} 
?>
and the only thing printing to screen is
the word 'Array' ?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

My apologies for not dealing with the whole code in one go. Firstly, you will definitely find it worth your while to check the manual on each function you are trying to use as there are examples and user notes on how they work and what information they return.

Ok, now the problem at hand. Basically, when you use the file() function it returns an array which has a separate element for each line of the text file. Therefore to do the replacement you need, you have to either implode() the array and then do the replacement or loop through the array using something like foreach to perform the replacement on each line. You can't just do:

Code: Select all

$string = "$data";
to transform an array into a string.

An example:

Code: Select all

$data_array = file('news.dat');

// make the information from the text file into a string
$string = implode("\n", $data_array);

// do the replacements
$pattern = array('!\[img\](.+?)\[/img\]!i', '!\[URL=(.*?)\](.*)\[/URL\]!i'); 
$replacement = array('<img src="\\1">', '<a class="link3" //href="http://\\1">\\2</a>'); 
print preg_replace($pattern, $replacement, $data);
Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

BTW, if you have PHP 4.3.0 or greater you can use file_get_contents() instead of file() and implode() to put all of the data in the file into a string.

Mac
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Dear Site Admin,
Thank you very much.
I used the file_get_contents and it worked to perfection.
I feel a need to justify myself...I did read all about the preg function...this is where I found the idea of the $pattern, and $replacement etc.
However the examples provided all dealt with the replacement of specific characters rather than the replacement of any occurences of words within tags.
Probably not a problem to an intermediate or above PHP user, but this confused the hell out of me.
I learnt what delimiters were in the process though,and to implode arrays rather that just explode, so the days of searching were of some use.
Anyway cheers, much appreciated help.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Whilst I am totally monopolising your time, another error has appeared...
the URL is added to the URL already in the address bar,a d so obviosuly this address does not exsist, why is the URL not being created properly?
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

Hello Twig, and any future users who are looking at this thread!
I have made a correction to the code - the links now work:

Code: Select all

<?PHP 
$data = file_get_contents('news.dat'); 
{ 
$string = "$data"; 
$pattern = array('!\[img\](.+?)\[/img\]!i', '!\[URL=(.*?)\](.*)\[/URL\]!i'); 
$replacement = array('<img src="\\1">', '<a class="link3"  href="http://\\1">\\2</a>'); 
print preg_replace($pattern, $replacement, $data);
} 
?>
Cheers for all the help I've had.
Post Reply