Page 1 of 1
str_replace not working in array
Posted: Mon Jul 21, 2003 2:57 am
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?
Posted: Mon Jul 21, 2003 4:27 am
by twigletmac
str_replace() does not take a pattern - it is looking for the exact text:
Code: Select all
!/\\їimg\\](.+?)\\ї\\/img\\]/!i
which does not exist in your text file.
Do you mean to use
preg_replace()?
Mac
Posted: Mon Jul 21, 2003 4:51 am
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...
Posted: Mon Jul 21, 2003 5:05 am
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
їimg](.+?)ї/img]/!i, !/їURL=(.*?)](.*)ї/URL]
not
Code: Select all
їimg](.+?)ї/img]
// and
їURL=(.*?)](.*)ї/URL]
Mac
Posted: Mon Jul 21, 2003 5:28 am
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' ?
Posted: Mon Jul 21, 2003 5:37 am
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:
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
Posted: Mon Jul 21, 2003 5:39 am
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
Posted: Mon Jul 21, 2003 5:59 am
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.
Posted: Mon Jul 21, 2003 6:14 am
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?
Posted: Mon Jul 21, 2003 6:31 am
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.